为 rest API Node 服务器考虑以下代码。
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);
});
使用 Node 运行该服务器:
$ node echo.js
restify listening at http://0.0.0.0:8080
它显示 0.0.0.0
是错误的。
任何人都可以告诉我如何在服务器打开时记录服务器的确切 IP 吗?
谢谢
更新: 在执行以下代码时,我得到下面提到的输出,这又很困难,选择哪个 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);
});
输出:
{ '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
最佳答案
可以给listen方法设置真实地址,见
http://mcavage.github.io/node-restify/#Server-API和 http://nodejs.org/docs/latest/api/net.html#net_server_listen_path_callback
server.address()
可能返回服务器绑定(bind)的地址:
http://nodejs.org/docs/latest/api/net.html#net_server_address
关于node.js - 如何在监听时获取 Restify 服务器的确切 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609499/