How can I get the clients IP address from the http req object?
IE:
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('../../lib/node-http-proxy');
//
// Http Server with proxyRequest Handler and Latency
//
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
// GET IP address here
// var ip = ??
var buffer = httpProxy.buffer(req);
setTimeout(function () {
proxy.proxyRequest(req, res, {
port: 9000,
host: 'localhost',
buffer: buffer
});
}, 200);
}).listen(8004);
It should just be req.connection.remoteAddress
That is usually the correct location to get the client's IP address, but not always. If you are using Nginx, Apache, or another reverse proxy in front of node.js you may have to get the IP address from req.headers. Common names for the header with the remote IP address include "X-Remote-IP" or "X-Originating-IP" but different servers use different header names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With