I have a legit business need to test the way our server works using a fake IP address. We use request.ip
in a bunch of different places and I'd rather not have to replace every single instance of "request.ip" with the fake IP address, so I figured I would just redefine it at the application's entrypoint (the start of the callback chain). However, I ran into some unexpected behavior when request.ip
apparently changed itself back to "127.0.0.1"!
This is the (CoffeeScript) code I inserted into the "handle" method:
spanishIp ='37.35.128.128'
request.ip = spanishIp
console.log "ip: #{request.ip} / #{spanishIp}"
Resulting in the following log:
ip: 127.0.0.1 / 37.35.128.128
Evidently, request.ip
got redefined back to my actual IP address (127.0.0.1) in between setting it to "37.35.128.128" and the console.log statement.
When I moved my code one step further into the callback chain, it worked as expected.
Is it possible that Node/Express has some internal mechanism that repeatedly sets request.ip
to the user's actual IP address at runtime?