Node.js v4.x HTTP Server One Liner
One liner to create a Node.js HTTP server:
|
|
Also see the previous one liners for older Node.js versions.
One liner to create a Node.js HTTP server:
|
|
Also see the previous one liners for older Node.js versions.
One liner to create a Node.js HTTP server:
|
|
Print out the process ID instead:
|
|
This is useful with the worker process when implementing the cluster module.
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: