How to get current domain address in sails.js

前端 未结 6 1452
再見小時候
再見小時候 2021-01-18 22:23

I trying to get current web address using sails.js.

I tried the following:

req.param(\'host\') and req.param(\'X-Forwarded-Protocol\') 
6条回答
  •  深忆病人
    2021-01-18 23:15

    I have tried some examples I have found in google but nothing seemed to work at least in my local machine.

    1. Using this in my local machine returned:

      req.ip;  -->  ::1
      
    2. Using this in my local machine returned:

      req.headers['x-forwarded-for'];  -->  undefined
      
    3. Finally, using this in my local machine returned:

      var paramIp = req.headers['x-forwarded-for'] ||
                  req.connection.remoteAddress ||
                  req.socket.remoteAddress ||
                  req.connection.socket.remoteAddress;  -->  ::1
      

    Nothing seemed to work. Then I tried in production environment and the first example returned:

    req.ip;  -->  ::ffff:10.155.43.243
    

    It seems to be working but as you can see it is an IPv6 ip address so that's not what I wanted.

    And the example number 3 returned:

    187.214.247.196
    

    So that's excellent because that's what I needed.

    All I have to said is that the example number 3 worked fine but just in production environment (using Heroku) not for development in my local machine.

提交回复
热议问题