Problem:
Get undefined problem with req.connection.remoteAddress.
The reason for this behavior is that iisnode acts as a reverse proxy between the client and the node process. The req.connection represents the connection between iisnode and node.exe, not between the client's machine and iisnode. Furthermore, the connection between iisnode and node.exe is based on named pipes not TCP, and as such does not expose many of the concepts specific to TCP like the IP address.
Solution:
The fix for this issue is to add support for X-Forwarded-For request header to iisnode.
This is how to enable X-Forwarded-For support through web.config:
<configuration>
<system.webServer>
<!-- ... -->
<iisnode enableXFF="true" />
</system.webServer>
</configuration>
This is the code node.js application can use to read the value of the header:
var http = require('http');
http.createServer(function (req, res) {
var xff = req.headers['x-forwarded-for'];
// ...
}).listen(process.env.PORT);
UPDATE: Now, we are hosting node.js applications in IIS on Windows via httpPlatformHandler, which supports X-Forwarded-For by default, so you don't need to enable X-Forwarded-For in web.config anymore, read client IP address through req.headers['x-forwarded-for'] directly in your code.
Article ID: 2314, Created: June 11, 2025 at 2:26 AM, Modified: February 6 at 10:08 PM