Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get user info with node.js

I am using express.js

I should learn user's browser name, browser language, country. How can i learn these ?

I tried useragent but i think it just give browser.

like image 851
Kerem Çakır Avatar asked Jan 31 '18 13:01

Kerem Çakır


People also ask

How do you find IP address in Nodejs?

The easiest way to find your IP address in Node. js is to pull the client IP address from the incoming HTTP request object. If you are running Express in your Node app, this is very easy to do. Access the socket field of the Express request object, and then look up the remoteAddress property of that field.

Can you console log in node JS?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

How do I know if a user is logged in node JS?

connect('mongodb://localhost/nyg', {useNewUrlParser: true}); const Schema = mongoose. Schema; const userSchema = new Schema({ email: String, password: String }); const User = mongoose. model('users', userSchema); app. post('/register', async (req, res, next) => { const user = await User.


1 Answers

You can get a bunch of information from the request headers. The user country will be more difficult, you will likely need to look it up from the IP address of the request. NB: This is not perfectly reliable and of course depends on getting the original request, not any proxy server address. You can use a library like geoip-lite for this (https://www.npmjs.com/package/geoip-lite).

I'd do something like:

var app = express();
app.set('trust proxy', true);

var geoip = require('geoip-lite');

app.get('/test', function(req, res){

    console.log('Headers: ' + JSON.stringify(req.headers));
    console.log('IP: ' + JSON.stringify(req.ip));

    var geo = geoip.lookup(req.ip);

    console.log("Browser: " + req.headers["user-agent"]);
    console.log("Language: " + req.headers["accept-language"]);
    console.log("Country: " + (geo ? geo.country: "Unknown"));
    console.log("Region: " + (geo ? geo.region: "Unknown"));

    console.log(geo);

    res.status(200);
    res.header("Content-Type",'application/json');
    res.end(JSON.stringify({status: "OK"}));
});

The request headers will contain a bunch of useful stuff, an example:

Headers: 
{
    "host": "testhost:3001",
    "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.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",
    "upgrade-insecure-requests": "1"
}

An example of the geo object would be:

{ 
  country: 'US',
  region: 'FL',
  city: 'Tallahassee',
  ll: [ 30.5252, -84.3321 ],
  metro: 530,
  zip: 32303 
}
like image 127
Terry Lennox Avatar answered Nov 17 '22 16:11

Terry Lennox