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
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
The easiest way to find your IP address in Node. js is to pull the client IP address from the incoming HTTP request object. If you are running Express in your Node app, this is very easy to do. Access the socket field of the Express request object, and then look up the remoteAddress property of that field.
A restify server object is the main interface through which you will register routes and handlers for incoming requests.
restify optimizes for introspection and performance, and is used in some of the largest Node. js deployments on Earth.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With