How To Use Ipinfo.io In Node.js And Express (and Heroku) Application Properly
I want to block users from some specific countries, and found that ipinfo.io serves the purpose. But when I tried to use it in my Node.js and Express application, it looks like it
Solution 1:
https://devcenter.heroku.com/articles/http-routing says:
"X-Forwarded-For: the originating IP address of the client connecting to the Heroku router"
So use:
req.headers['x-forwarded-for']
instead of
req.ip
If you use req.ip
, you are "asking" ipinfo.io about the IP of the router.
Solution 2:
It looks like you're behind a proxy, and you're passing an internal IP address to ipinfo.io. You should enable the "trust proxy" setting so that express pulls the correct IP from the X-Forwarded-For
header, eg:
app.enable('trust proxy')
And then you can use req.ip will correctly return the client's IP. You probably shouldn't use req.headers['x-forwarded-for']
directly because that could contain a list of IPs.
More details are available at http://expressjs.com/guide.html#proxies
Post a Comment for "How To Use Ipinfo.io In Node.js And Express (and Heroku) Application Properly"