How to determine a user's IP address in node

后端 未结 19 869
天命终不由人
天命终不由人 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: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;
    

提交回复
热议问题