Nodejs ip address result ::1

I have a realy interesting problem. I have a web site and i want to get client ip address. I found some solition but none of them work. I am using nginx.

i am using expressjs

app.post("/api/test",(req, res)=>{
console.log(req.header('x-forwarded-for')) // result "::1"
console.log(req.connection.remoteAddress) // result "::1"
console.log(req.ip) // result "::1"
})

I try yo use 3 party freamwork but result same.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If you are working on localhost this is normal try logging this on server you will get the address of the user.

Or you might be running nginx or similar reverse proxy in front of your node server in this case you should set proper headers

for nginx you need this ones

proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

check herefor more info

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply