Javascript 如何在 node.js 中获取有关客户端的信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3895434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:32:35  来源:igfitidea点击:

How to get information about the client in node.js

javascriptreal-timenode.js

提问by oscarm

in this very simple example:

在这个非常简单的例子中:

var sys = require("sys"),
    http = require("http");

http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end("Hello World!");
}).listen(8080);

sys.puts("Server running at http://localhost:8080/");

1.) What kind of information can I get from the client? like browser, screen resolution, etc?

1.) 我可以从客户那里得到什么样的信息?比如浏览器、屏幕分辨率等?

2.) How can I send information from the client to server, like parameter?

2.) 如何从客户端向服务器发送信息,比如参数?

thanks!

谢谢!

采纳答案by leeight

You can't get the screen resolution information, but you can get the user agent from Request Header "User-Agent"

无法获取屏幕分辨率信息,但可以从请求头“User-Agent”中获取用户代理

回答by cyberwombat

1) Referrer URL, IP address, User Agent, screen size and other stats. You can also get geo location but that is more involved.

1) 引用 URL、IP 地址、用户代理、屏幕大小和其他统计信息。您还可以获取地理位置,但这涉及更多。

2) Some data is available in the headers so these are sent on every request - other data such as screen size is a little trickier so you'll want to make an ajax request to send that along.

2) 标头中提供了一些数据,因此这些数据会在每个请求中发送 - 其他数据(例如屏幕大小)有点棘手,因此您需要发出 ajax 请求以将其发送。

// Somewhere on your page(s) - here we use jQuery
$(document).ready(function(){   
  // Check if they have been logged
  if ($.cookie('logged') == null ){
    // Send screen size and whatever else that is not available from headers
    $.post('/logger', { width: screen.width, height: screen.height }, function(res) {
      // Set cookie  for 30 days so we don't keep doing this
       $.cookie('logged', true, { expires: 30 }); 
    });
  } 
 });  

// Server side - example is an Express controller
exports.logger = function(req, res) {
  var user = {
    agent: req.header('user-agent'(, // User Agent we get from headers
    referrer: req.header('referrer'), //  Likewise for referrer
    ip: req.header('x-forwarded-for') || req.connection.remoteAddress, // Get IP - allow for proxy
    screen: { // Get screen info that we passed in url post data
      width: req.param('width'),
      height: req.param('height')
    }
  };
  // Store the user in your database
  // User.create(user)...
  res.end();
}

回答by NG.

Have you read the API docs? The req object is a http.ServerRequest object as documented there. It's HTTP, and such things like resolution are not part of the protocol. What you can get is a user-agent, and from there you might be able to retrieve more information using another service.

你读过API 文档吗?req 对象是一个 http.ServerRequest 对象,如此处所述。它是 HTTP,诸如解析之类的东西不是协议的一部分。您可以获得一个用户代理,然后您可以从那里使用其他服务检索更多信息。

Remember that node.js is a standalone app - it's not running in a browser - it's an HTTP Server application that is running in a JS interpreter.

请记住,node.js 是一个独立的应用程序——它不在浏览器中运行——它是一个在 JS 解释器中运行的 HTTP 服务器应用程序。