How to Get Client IP Address in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Create Separate Routes File in Node JS Express?
- How to Get Query String Value in Node.js?
- Node JS Express Route with Parameters Example
- How to Create Route in Node JS Express JS?
- How to Generate 4,6,8,10 Digit Random number in Node JS?
- Node JS Read and Write Json File Example
- Node JS Find and Update Object in Array Example
- How to Get Data from Json File in Node JS?
- How to Remove Element from Array in Node JS?
- How to Push Element in Array in Node JS?
- How to use Underscore JS in Node JS App?