Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js server.address().address returns ::

If I remember correctly it used to display "localhost" a few days ago. I am not sure what had changed that made server.address().address return double colons (::) instead. I read here that it returns an IPv6 address (::) if it is available but it's disabled on my PC. https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback

like image 802
Jake Avatar asked Nov 22 '15 10:11

Jake


People also ask

How do I find my node js server address?

address() is an inbuilt application programming interface of class Socket within tls module which is used to get the bound address of the server. Parameters: This method does not accept any parameter. Return Value: This method return the bound address containing the family name, and port of the server.

How get full url in Express?

To get the full URL in Express and Node. js, we can use the url package. to define the fullUrl function that takes the Express request req object. And then we call url.

Which method is used to send the data from Nodejs server to client?

Methods to send response from server to client are: Using send() function. Using json() function.


1 Answers

As the docs say,

Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. A port value of zero will assign a random port.

So, the following code would print running at http://:::3456:

var express      = require('express'); var app          = express(); var server = app.listen(3456, function () {     var host = server.address().address;     var port = server.address().port;     console.log('running at http://' + host + ':' + port) }); 

But if you add an explicit hostname:

var server = app.listen(3456, "127.0.0.1", function () { 

It would print what you want to see: running at http://127.0.0.1:3456

Also, you might want to use some IP lib as pointed in this answer

like image 92
Alexander Mikhalchenko Avatar answered Sep 17 '22 08:09

Alexander Mikhalchenko