سؤال

Doing the following:

require ("http").createServer (function (req, res) {
   /* get the browser unique id */
}).listen (8080);

Does the req object contain something unique for each browser session?

For example: having two browser tabs, the unique field should be the same. But accessing the same page from two different browsers (there should be two different values: one for each browser).

I suppose that such a field is presend in req.socket object. When using socket.io library I there is a client.id field.

Can I do this without setting cookies on the client side?

هل كانت مفيدة؟

المحلول

As is written in Node.JS API documentation, the req paramenter is used to access headers, data and response status(see HTTP response codes).

Inspecting the req object for each browser is too hard because of the number of properties and methods(a lot of discussion here). But if you would inspect the headers(req.headers) you would see a small difference between req objects. For example I opened a new Google Chrome Icognito Window and a new Private Windows in Firefox and these the headers:

{ host: 'lvh.me:8000',
  'user-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'accept-language': 'en-US,en;q=0.5',
  'accept-encoding': 'gzip, deflate',
  connection: 'keep-alive' }
{ host: 'lvh.me:8000',
  connection: 'keep-alive',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8' }

So, the only properties that are same in both objects are host and connection. But the host could be different if you call the server with a different subdomain(For example: www)

If you could inspect the whole object, probably you would find out more about each browser.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top