How to Get Client IP Address in Node JS?

By Hardik Savani June 17, 2021 Category : Node JS

Today, i would like to show you node js get client ip address. you can understand a concept of node js express get client ip address. if you want to see example of node js get external ip address then you are a right place. We will use get real ip address node js. Let's get started with how to get client ip address in node js.

let's see bellow examples to getting client ip address in node js.

Example 1:

server.js

var express = require('express');

var app = express();

app.get('/',function(request, response) {

var idAddress = request.connection.remoteAddress;

console.log(idAddress);

});

app.listen(3000, () => console.log(`App listening on port 3000`))

Example 2:

server.js

var express = require('express');

var app = express();

app.get('/',function(request, response) {

var idAddress = request.header('x-forwarded-for') || request.connection.remoteAddress;

console.log(idAddress);

});

app.listen(3000, () => console.log(`App listening on port 3000`))

Example 3:

install npm package

npm install request-ip --save

server.js

var express = require('express');

var app = express();

var requestIp = require('request-ip');

app.get('/',function(request, response) {

var clientIp = requestIp.getClientIp(request);

console.log(clientIp);

});

app.listen(3000, () => console.log(`App listening on port 3000`))

i hope it can help you...

Tags :
Shares