Skip to main content

Server

class Server
extends EventEmitter

This class is used to create a TCP or IPC server.

Constructors Jump to heading

new
Jump to headingServer(connectionListener?: (socket: Socket) => void)
new
Jump to headingServer(
options?: ServerOpts,
connectionListener?: (socket: Socket) => void,
)

Properties Jump to heading

readonly
Jump to headinglistening: boolean

Indicates whether or not the server is listening for connections.

Set this property to reject connections when the server's connection count gets high.

It is not recommended to use this option once a socket has been sent to a child with child_process.fork().

Calls Server.close() and returns a promise that fulfills when the server has closed.

Jump to headingaddListener(
event: string,
listener: (...args: any[]) => void,
): this

events.EventEmitter

  1. close
  2. connection
  3. error
  4. listening
  5. drop
Jump to headingaddListener(
event: "close",
listener: () => void,
): this
Jump to headingaddListener(
event: "connection",
listener: (socket: Socket) => void,
): this
Jump to headingaddListener(
event: "error",
listener: (err: Error) => void,
): this
Jump to headingaddListener(
event: "listening",
listener: () => void,
): this
Jump to headingaddListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this

Returns the bound address, the address family name, and port of the server as reported by the operating system if listening on an IP socket (useful to find which port was assigned when getting an OS-assigned address):{ port: 12346, family: 'IPv4', address: '127.0.0.1' }.

For a server listening on a pipe or Unix domain socket, the name is returned as a string.

const server = net.createServer((socket) => {
  socket.end('goodbye\n');
}).on('error', (err) => {
  // Handle errors here.
  throw err;
});

// Grab an arbitrary unused port.
server.listen(() => {
  console.log('opened server on', server.address());
});

server.address() returns null before the 'listening' event has been emitted or after calling server.close().

Jump to headingclose(callback?: (err?: Error) => void): this

Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event. The optional callback will be called once the 'close' event occurs. Unlike that event, it will be called with an Error as its only argument if the server was not open when it was closed.

Jump to headingemit(
event: string | symbol,
...args: any[],
): boolean
Jump to headingemit(event: "close"): boolean
Jump to headingemit(
event: "connection",
socket: Socket,
): boolean
Jump to headingemit(
event: "error",
err: Error,
): boolean
Jump to headingemit(event: "listening"): boolean
Jump to headingemit(
event: "drop",
data?: DropArgument,
): boolean
Jump to headinggetConnections(cb: (
error: Error | null,
count: number,
) => void
): void

Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.

Callback should take two arguments err and count.

Jump to headinglisten(
port?: number,
hostname?: string,
backlog?: number,
listeningListener?: () => void,
): this

Start a server listening for connections. A net.Server can be a TCP or an IPC server depending on what it listens to.

Possible signatures:

  • server.listen(handle[, backlog][, callback])
  • server.listen(options[, callback])
  • server.listen(path[, backlog][, callback]) for IPC servers
  • server.listen([port[, host[, backlog]]][, callback]) for TCP servers

This function is asynchronous. When the server starts listening, the 'listening' event will be emitted. The last parameter callbackwill be added as a listener for the 'listening' event.

All listen() methods can take a backlog parameter to specify the maximum length of the queue of pending connections. The actual length will be determined by the OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on Linux. The default value of this parameter is 511 (not 512).

All Socket are set to SO_REUSEADDR (see socket(7) for details).

The server.listen() method can be called again if and only if there was an error during the first server.listen() call or server.close() has been called. Otherwise, an ERR_SERVER_ALREADY_LISTEN error will be thrown.

One of the most common errors raised when listening is EADDRINUSE. This happens when another server is already listening on the requestedport/path/handle. One way to handle this would be to retry after a certain amount of time:

server.on('error', (e) => {
  if (e.code === 'EADDRINUSE') {
    console.error('Address in use, retrying...');
    setTimeout(() => {
      server.close();
      server.listen(PORT, HOST);
    }, 1000);
  }
});
Jump to headinglisten(
port?: number,
hostname?: string,
listeningListener?: () => void,
): this
Jump to headinglisten(
port?: number,
backlog?: number,
listeningListener?: () => void,
): this
Jump to headinglisten(
port?: number,
listeningListener?: () => void,
): this
Jump to headinglisten(
path: string,
backlog?: number,
listeningListener?: () => void,
): this
Jump to headinglisten(
path: string,
listeningListener?: () => void,
): this
Jump to headinglisten(
options: ListenOptions,
listeningListener?: () => void,
): this
Jump to headinglisten(
handle: any,
backlog?: number,
listeningListener?: () => void,
): this
Jump to headinglisten(
handle: any,
listeningListener?: () => void,
): this
Jump to headingon(
event: string,
listener: (...args: any[]) => void,
): this
Jump to headingon(
event: "close",
listener: () => void,
): this
Jump to headingon(
event: "connection",
listener: (socket: Socket) => void,
): this
Jump to headingon(
event: "error",
listener: (err: Error) => void,
): this
Jump to headingon(
event: "listening",
listener: () => void,
): this
Jump to headingon(
event: "drop",
listener: (data?: DropArgument) => void,
): this
Jump to headingonce(
event: string,
listener: (...args: any[]) => void,
): this
Jump to headingonce(
event: "close",
listener: () => void,
): this
Jump to headingonce(
event: "connection",
listener: (socket: Socket) => void,
): this
Jump to headingonce(
event: "error",
listener: (err: Error) => void,
): this
Jump to headingonce(
event: "listening",
listener: () => void,
): this
Jump to headingonce(
event: "drop",
listener: (data?: DropArgument) => void,
): this
Jump to headingprependListener(
event: string,
listener: (...args: any[]) => void,
): this
Jump to headingprependListener(
event: "close",
listener: () => void,
): this
Jump to headingprependListener(
event: "connection",
listener: (socket: Socket) => void,
): this
Jump to headingprependListener(
event: "error",
listener: (err: Error) => void,
): this
Jump to headingprependListener(
event: "listening",
listener: () => void,
): this
Jump to headingprependListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this
Jump to headingprependOnceListener(
event: string,
listener: (...args: any[]) => void,
): this
Jump to headingprependOnceListener(
event: "close",
listener: () => void,
): this
Jump to headingprependOnceListener(
event: "connection",
listener: (socket: Socket) => void,
): this
Jump to headingprependOnceListener(
event: "error",
listener: (err: Error) => void,
): this
Jump to headingprependOnceListener(
event: "listening",
listener: () => void,
): this
Jump to headingprependOnceListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this

Opposite of unref(), calling ref() on a previously unrefed server will not let the program exit if it's the only server left (the default behavior). If the server is refed calling ref() again will have no effect.

Calling unref() on a server will allow the program to exit if this is the only active server in the event system. If the server is already unrefed callingunref() again will have no effect.

Back to top