Reputation: 89
I want to ask, I have an application using expressjs now I am asked to display the ip of the user who accesses the application but it always displays the ip of the server, which wants to display the ip of the user accessing the application or the laptop accessing the application maybe someone can help me thanks in advance
Upvotes: 0
Views: 3978
Reputation: 114004
If you are getting 127.0.0.1 it means you are running express behind a web server, perhaps Apache or Nginx or IIS. If so you need to understand that your users do not connect to your express application. Instead your web server connects to your express application so it is correct that the client's (web server's) ip address is 127.0.0.1.
You cannot fix this in express alone since in this case express has no direct connection to your user's machine. You need to configure your web server to forward the client's IP address to express. How you do this depends on your web server. Obviously Apache, Nginx, IIS, Lighttpd etc. all have different configuration formats.
The most common way to do this is to configure your web server to set the X-Forwarded-For
header. Then in express you need to read the x-forwarded-for
header in req
.
Google your web server's name and "x-forwarded-for" for how to configure your server to do this.
Upvotes: 1
Reputation: 1
maybe this package can help you https://www.npmjs.com/package/ext-ip
let extIP = require("ext-ip")();
extIP.get().then(ip => {
console.log(ip);
}, err => {
console.error(err);
});
Upvotes: 0