5

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 ??

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Eyad Farra
  • 4,403
  • 1
  • 24
  • 26

6 Answers6

8

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;
};
sgress454
  • 24,870
  • 4
  • 74
  • 92
7

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

Melvin
  • 5,798
  • 8
  • 46
  • 55
1

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.

alexventuraio
  • 8,126
  • 2
  • 30
  • 35
0

req.host should contain the host value.

Nervetattoo
  • 568
  • 3
  • 10
  • There is no host object under req , its retuns undefined. – Eyad Farra May 13 '14 at 08:17
  • 1
    You mean req.headers.host. but this returns always localhost :( – Eyad Farra May 13 '14 at 08:18
  • I just tested this to make sure, and it works fine for me on sails 0.9.x – Nervetattoo May 13 '14 at 08:19
  • 1
    Looking through the 0.10 code it seems that this shold work for sure. You could also inspect **sails.config.host** which should hold the *configured* hostname. If you don't access the node.js process directly then for example nginx could convolute this and thus mess up the detection in sails.js – Nervetattoo May 13 '14 at 08:25
0

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.

  • How can I implement that in an service module stored in /api/services/someservice.js that has no "req" param? Because it is not associated to a url request. – alexventuraio Nov 18 '15 at 01:40
  • It depends how you build the service. In some cases, I simply pass `baseURL` as a parameter into he service (assuming it's called from a controller). In other cases, I'll do something silly in the service like this: `var baseUrl = (process.env.NODE_ENV=='development') ? 'http://localhost:133/' : 'https://myappexamplesite.com'` –  Dec 18 '15 at 04:56
0

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