How to determine a user's IP address in node

后端 未结 19 873
天命终不由人
天命终不由人 2020-11-22 12:46

How can I determine the IP address of a given request from within a controller? For example (in express):

app.post(\'/get/ip/address\', function (req, res) {         


        
相关标签:
19条回答
  • 2020-11-22 13:14

    If using express...

    req.ip

    I was looking this up then I was like wait, I'm using express. Duh.

    0 讨论(0)
  • 2020-11-22 13:15

    Following Function has all the cases covered will help

    var ip;
    if (req.headers['x-forwarded-for']) {
        ip = req.headers['x-forwarded-for'].split(",")[0];
    } else if (req.connection && req.connection.remoteAddress) {
        ip = req.connection.remoteAddress;
    } else {
        ip = req.ip;
    }console.log("client IP is *********************" + ip);
    
    0 讨论(0)
  • 2020-11-22 13:19

    I realize this has been answered to death, but here's a modern ES6 version I wrote that follows airbnb-base eslint standards.

    const getIpAddressFromRequest = (request) => {
      let ipAddr = request.connection.remoteAddress;
    
      if (request.headers && request.headers['x-forwarded-for']) {
        [ipAddr] = request.headers['x-forwarded-for'].split(',');
      }
    
      return ipAddr;
    };
    

    The X-Forwarded-For header may contain a comma-separated list of proxy IPs. The order is client,proxy1,proxy2,...,proxyN. In the real world, people implement proxies that may supply whatever they want in this header. If you are behind a load balancer or something, you can at least trust the first IP in the list is at least whatever proxy some request came through.

    0 讨论(0)
  • 2020-11-22 13:21

    In node 10.14 , behind nginx, you can retrieve the ip by requesting it through nginx header like this:

    proxy_set_header X-Real-IP $remote_addr;
    

    Then in your app.js:

    app.set('trust proxy', true);
    

    After that, wherever you want it to appear:

    var userIp = req.header('X-Real-IP') || req.connection.remoteAddress;
    
    0 讨论(0)
  • 2020-11-22 13:22
    var ip = req.headers['x-forwarded-for'] || 
         req.connection.remoteAddress || 
         req.socket.remoteAddress ||
         (req.connection.socket ? req.connection.socket.remoteAddress : null);
    

    Note that sometimes you can get more than one IP address in req.headers['x-forwarded-for']. Also, an x-forwarded-for header will not always be set which may throw an error.

    The general format of the field is:

    x-forwarded-for: client, proxy1, proxy2, proxy3

    where the value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. In this example, the request passed through proxy1, proxy2, and then proxy3. proxy3 appears as remote address of the request.

    This is the solution suggested by Arnav Gupta with a fix Martin has suggested below in the comments for cases when x-forwarded-for is not set :

    var ip = (req.headers['x-forwarded-for'] || '').split(',').pop().trim() || 
             req.connection.remoteAddress || 
             req.socket.remoteAddress || 
             req.connection.socket.remoteAddress
    
    0 讨论(0)
  • 2020-11-22 13:22

    Simple get remote ip in nodejs:

    var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
    
    0 讨论(0)
提交回复
热议问题