How to get current domain address in sails.js

折月煮酒 提交于 2019-12-01 17:35:50

问题


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

I tried the following:

req.param('host') and req.param('X-Forwarded-Protocol') 

returns undefined.

req.headers.host

returns localhost. but my domain is not localhost.

How to get it ??


回答1:


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;
};



回答2:


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




回答3:


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.




回答4:


req.host should contain the host value.




回答5:


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.




回答6:


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);
  }
...


来源:https://stackoverflow.com/questions/23625935/how-to-get-current-domain-address-in-sails-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!