How to Get User’s IP Details in Express

Express.js provides the user’s IP information in the request object which can be accessed using req.connection.remoteAddress. If you are using a reverse proxy in the front of the Express web server ( You should be in the production server) you can retrieve the IP address in the x-forwarded-for HTTP header, assuming you have done the configuration of your reverse proxy server correctly.

Using Express to get the IP address of a client in Node.js

The following code shows how to extract the client’s IP address in the Express router using Node.js:

const express = require('express');
const app = express();
const router = express.Router();

router.get('/', (req,res) => {
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  console.log(ip); // ip address of the user
});

app.use('/', router);
app.listen(3000);

In the above code, we have first imported the express module, then created its instance app, then created an express router to define a route to handle the incoming request, and then we have created a route using that router to handle a get request for the root path then mounting the router and finally use listen() method so that application can be listening on port 3000.

Getting User’s IP Details

In order to extract the user’s information such as location, you can use a free node module geoip-lite. You can install this module using the node package manager.

Below is the command to install geoip-lite module:

npm install --save geoip-lite

To extract location detail, pass the IP of the user in the lookup function fetched from the geoip-lite module.

const express = require('express');
const app = express();
const { lookup } = require('geoip-lite');
const router = express.Router();

router.get('/', (req,res) => {
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
  console.log(ip); // ip address of the user
  console.log(lookup(ip)); // location of the user
});

app.use('/', router);
app.listen(3000);

If you are running it on the local machine, you cannot get the location data. To test it out, just hardcode your IP address in the ip variable.

Simply replace the line const ip = req.headers[‘x-forwarded-for’] || req.connection.remoteAddress; with const ip = ‘YOUR_TEST_IP_ADDRESS’;, where ‘YOUR_TEST_IP_ADDRESS’ is the IP address you want to use for testing.

You can also use a cloud-based development environment like Replit to run and test the code.

Output:

49.36.211.187
{
  range: [ 824492032, 824508415 ],
  country: 'IN',
  region: '',
  eu: '0',
  timezone: 'Asia/Kolkata',
  city: '',
  ll: [ 20, 77 ],
  metro: 0,
  area: 1000
}

See here we got the IP address as well as more information related to that IP.

Summary 

IP stands for Internet Protocol. To get the IP address, you can make use of the HTTP request object using Express. The request object contains various request-related information, including IP address. Moreover, if you are using a reverse proxy, you can get the real user IP address in the x-forwarded-for header. You can use lookup function from the geoip-lite module, to extract more details about that received IP address. You can install geoip-lite module using NPM.

Hope now you got the idea of accessing the IP address. If you’re new to Express.js or Node.js, consider reading the tutorial Node.js and Express Tutorial for Beginners.

Reference

https://www.npmjs.com/package/geoip-lite

Pankaj Kumar
Pankaj Kumar
Articles: 209