简体   繁体   English

如何在侦听时获取Restify服务器的确切IP地址?

[英]How to get the exact IP address of the Restify server on listen?

Consider the following code for rest API node server. 请考虑以下用于其余API节点服务器的代码。

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. 它显示0.0.0.0这是错误的。

Can anyone the me how to console log the exact IP of the server when its turned on? 任何人都可以在开启时如何控制台登录服务器的确切IP?

Thanks 谢谢


Update : On executing the following code I am getting the below mentioned output, which is again makes difficult, which IP to pick? 更新 :在执行下面的代码时,我得到了下面提到的输出,这再次变得困难,选择哪个IP?

  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 

You can set the real address to the listen method, see 您可以将实际地址设置为listen方法,请参阅

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

server.address() may return the address the server was bound to: server.address()可能会返回服务器绑定的地址:

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

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 有相同的问题并找出问题,基本上你可以传递另一个参数到.listen()函数,这应该是你的服务器IP地址

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. 为了获得您的服务器IP地址,请在终端中执行“traceroute www.WEBSITE_URL.com”。

let me know if you still have an issue. 如果您还有问题,请告诉我。

thanks Mahdi 谢谢马赫迪

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM