I'm having a difficult time finding how to access the IP address of the REST client from a route.
server.get('api/foo', function(req, res, next) {
// How can I access the IP address of the requester from here?
}
I'm having a difficult time finding how to access the IP address of the REST client from a route.
server.get('api/foo', function(req, res, next) {
// How can I access the IP address of the requester from here?
}
The other answers won't work behind a proxy, you'll get the proxy server address in those cases.
req.headers['x-forwarded-for'] || req.connection.remoteAddress;
Will work behind a proxy if the proxy sets the original IP in x-forwarded-for
header which many do by default and you can add to something like nginx very easily.
© 2022 - 2024 — McMap. All rights reserved.
req
object. – Rentsch