Locate HTTP Port Number from http.Server Instance
This is a bare minimal HTTP server:
|
|
If the port
number is evaluated to be falsy, such as 0
, null
, undefined
or an empty string, a random port will be assigned.
Begin accepting connections on the specified
port
andhostname
. If thehostname
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. - https://iojs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
We can use command line tool such as netstat
to find out the port number:
|
|
However, it will be very convenient without relying on external tools.
Luckily, we can find out the port number by using the address
method:
|
|
The result will be similar to:
|
|
Furthermore, it works with all popular libraries that create an http.Server
instance:
|
|
They are all having the same result.
Finally, here is a Koa implementation that shows the port number the server is running at:
|
|