How to get the exact IP address of the Restify server on listen?
Asked Answered
C

3

5

Consider the following code for rest API node server.

var restify = require('restify');

var server = restify.createServer();

server.get('/echo/:name', function (req, res, next) {
  res.send({name: req.params.name});
  next();
});

server.listen(8080, function () {
  console.log('%s listening at %s', server.name, server.url);
});

Running up that server with node:

$ node echo.js
restify listening at http://0.0.0.0:8080

It shows 0.0.0.0 which is wrong.

Can anyone the me how to console log the exact IP of the server when its turned on?

Thanks


Update: On executing the following code I am getting the below mentioned output, which is again makes difficult, which IP to pick?
    rest_server.listen(config.appPort, function () {
      var adrs = require('os').networkInterfaces();
          console.log(adrs);
          console.log('%s listening at %s', rest_server.name, rest_server.url);
    });

Output:

{ 'Local Area Connection': [ { address: '192.168.3.60', family: 'IPv4', internal
: false } ],
  'Loopback Pseudo-Interface 1':
   [ { address: '::1', family: 'IPv6', internal: true },
     { address: '127.0.0.1', family: 'IPv4', internal: true } ],
  'isatap.TEST.ORG':
   [ { address: 'fe80::5efe:c0a8:33c',
       family: 'IPv6',
       internal: false } ] }
restify listening at http://0.0.0.0:8080
Caylacaylor answered 17/5, 2013 at 12:50 Comment(1)
In this case, you should pick 192.168.3.60 because it's not internal and IPv4.Phosphorescent
N
5

You can set the real address to the listen method, see

http://mcavage.github.io/node-restify/#Server-API and http://nodejs.org/docs/latest/api/net.html#net_server_listen_path_callback

server.address() may return the address the server was bound to:

http://nodejs.org/docs/latest/api/net.html#net_server_address

Nuclear answered 17/5, 2013 at 13:2 Comment(4)
server.address() is returning the same 0.0.0.0.. Any clue ? { address: '0.0.0.0', family: 'IPv4', port: 8080 }Caylacaylor
Updated my question based on your inputs. But still not able to get exactly what is required :(Caylacaylor
the best would to start listen by giving directly the hotname/address. It seems it cannot resolve the localhost name. Are you on windows?Nuclear
based on your answer I have derived a solution, just have a look at it.. Thanks for your time..Caylacaylor
I
5

Had the Same issue and figured out the problem, basically you can pass another parameter to the .listen() function and that should be your server IP address

server.listen(port, [host], [backlog], [callback])


server.listen(port, YOUR IP ADDRESS, function(){ });

for you to get your server ip address just do "traceroute www.WEBSITE_URL.com" in the terminal.

let me know if you still have an issue.

thanks Mahdi

Incest answered 4/11, 2013 at 4:32 Comment(0)
C
0

Got some solution, which is as follows.

Any improvements on the same is always welcomed.

var osDetails = require('os');

function getDynamicIP(calbck) {
    var ip;
    try {
        var adrs = osDetails.networkInterfaces();
        for (key in adrs) {
            if (adrs.hasOwnProperty(key)) {
                if (adrs[key][0].internal === false && adrs[key][0].family === 'IPv4') {
                    ip = adrs[key][0].address;
                    break;
                }
            }
        }
    }
    catch (e) {
        return calbck(e, null);
    }
    return calbck(null, ip);
}
Caylacaylor answered 22/5, 2013 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.