How to get current domain address in sails.js

前端 未结 6 1451
再見小時候
再見小時候 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 22:59

    Just to preface this answer: there is no reliable, cross-platform way to automatically detect the external URL of a running Sails app (or any other Node app). The getBaseUrl() method described below uses a combination of user-supplied and default configuration values to make a best guess at an app's URL. In mission-critical situations, you are advised to pre-determine the URL and save it in a custom environment-dependent configuration value (e.g. sails.config.appUrl) that you can reference elsewhere in the app.

    If you're on Sails >= v0.10.x, you can use sails.getBaseurl() to get the full protocol, domain and port that your app is being served from. Starting with Sails v0.10.0-rc6, this also checks the sails.config.proxyHost and sails.config.proxyPort, which you can set manually in your one of your config files (like config/local.js) if your app is being served via a proxy (e.g. if it's deployed on Modulus.io, or proxied through an Nginx server).

    Sails v0.9.x doesn't have sails.getBaseurl, but you can always try copying the code and implementing yourself, probably in a service:

    getBaseUrl

    function getBaseurl() {
      var usingSSL = sails.config.ssl && sails.config.ssl.key && sails.config.ssl.cert;
      var port = sails.config.proxyPort || sails.config.port;
      var localAppURL =
        (usingSSL ? 'https' : 'http') + '://' +
        (sails.getHost() || 'localhost') +
        (port == 80 || port == 443 ? '' : ':' + port);
    
      return localAppURL;
    };
    

    you'll notice this relies on sails.getHost(), which looks like:

    function getHost() {
      var hasExplicitHost = sails.config.hooks.http && sails.config.explicitHost;
      var host = sails.config.proxyHost || hasExplicitHost || sails.config.host;
      return host;
    };
    
    0 讨论(0)
  • 2021-01-18 23:03

    In Sails v0.10 you can access it through req.baseUrl or sails.getBaseurl() in your views..

    0 讨论(0)
  • 2021-01-18 23:05

    This is the code I use for getting the IP of any server based on Node.js and it works with Sails.js:

    var os = require('os');
    ....
      getIpAddress: function() {
        var ifaces = os.networkInterfaces();
        for (k in ifaces) {
          for (k2 in ifaces[k]) {
            var address = ifaces[k][k2];
            if (address.family == 'IPv4' && !address.internal) {
              return(address.address);
            }
          }
        }
        return(null);
      }
    ...
    
    0 讨论(0)
  • 2021-01-18 23:07

    req.host should contain the host value.

    0 讨论(0)
  • 2021-01-18 23:11

    On sails 0.10.x, you could do this:

    //inside controller action
    var baseURL = req.protocol + '://' + req.host;
    

    req.protocol will give you 'http' or 'https' (or whatever application protocol). req.host will be the host's header, e.g. 'sub.domain.com'

    As noted in a comment above, sails.getBaseURL() or any of it's variants will usually return localhost, even in a production environment.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题