How to get remote IP address with IISNode

Programming, error messages and sample code > Node.js
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);