从Http-Server中的请求获取IP地址

时间:2018-11-07 17:57:00

标签: node.js http npm

我有如下代码来获取IP地址和负载平衡:

var httpProxy = require("http-proxy");
var http = require('http');
var where=require('node-where');    
var proxy = httpProxy.createProxyServer({});
var server=http.createServer( function (req, res){
    console.log(req.headers['x-forwarded-for'] || req.connection.remoteAddress);

    var country,state
    where.is(req.connection.remoteAddress, function (err, result) {
        console.log(result);
        country=result.get('countryCode');
        // state=result.get('regionCode');
        if(country!='US')
        {
            proxy.web(req, res, {target: "http://localhost:3000"}, function (e){
                proxy.web(req, res, {target: "http://localhost:3001"});
            });
        }
        else
        {
            proxy.web(req, res, {target: "localhost:3001"}, function (e){
                proxy.web(req, res, {target: "localhost:3000"});
            });
        }
    });
});

server.listen(80);

但是,控制台日志仅返回::1。该IP地址在请求中不可见。

如何获取访问网站的机器的IP地址,以便根据IP地址所在的国家/地区进行负载平衡?

0 个答案:

没有答案
相关问题