node.jsip-addressclientip

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.


Solution

  • 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