Skip to main content
Server - node__net.d.ts - Node documentation
class Server
extends EventEmitter

Usage in Deno

```typescript import { Server } from "node:node__net.d.ts"; ```
This class is used to create a TCP or `IPC` server.

Constructors

new
Server(connectionListener?: (socket: Socket) => void)
new
Server(
options?: ServerOpts,
connectionListener?: (socket: Socket) => void,
)

Properties

connections: number
readonly
listening: 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()`.

Methods

[Symbol.asyncDispose](): Promise<void>
Calls Server.close() and returns a promise that fulfills when the server has closed.
addListener(
event: string,
listener: (...args: any[]) => void,
): this
events.EventEmitter 1. close 2. connection 3. error 4. listening 5. drop
addListener(
event: "close",
listener: () => void,
): this
addListener(
event: "connection",
listener: (socket: Socket) => void,
): this
addListener(
event: "error",
listener: (err: Error) => void,
): this
addListener(
event: "listening",
listener: () => void,
): this
addListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this
address():
AddressInfo
| string
| null
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. ```js 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()`.
close(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.
emit(
event: string | symbol,
...args: any[],
): boolean
emit(event: "close"): boolean
emit(
event: "connection",
socket: Socket,
): boolean
emit(
event: "error",
err: Error,
): boolean
emit(event: "listening"): boolean
emit(
event: "drop",
data?: DropArgument,
): boolean
getConnections(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`.
listen(
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 `callback`will 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](../.././node__net.d.ts/~/Socket) are set to `SO_REUSEADDR` (see [`socket(7)`](https://man7.org/linux/man-pages/man7/socket.7.html) 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 requested`port`/`path`/`handle`. One way to handle this would be to retry after a certain amount of time: ```js server.on('error', (e) => { if (e.code === 'EADDRINUSE') { console.error('Address in use, retrying...'); setTimeout(() => { server.close(); server.listen(PORT, HOST); }, 1000); } }); ```
listen(
port?: number,
hostname?: string,
listeningListener?: () => void,
): this
listen(
port?: number,
backlog?: number,
listeningListener?: () => void,
): this
listen(
port?: number,
listeningListener?: () => void,
): this
listen(
path: string,
backlog?: number,
listeningListener?: () => void,
): this
listen(
path: string,
listeningListener?: () => void,
): this
listen(
options: ListenOptions,
listeningListener?: () => void,
): this
listen(
handle: any,
backlog?: number,
listeningListener?: () => void,
): this
listen(
handle: any,
listeningListener?: () => void,
): this
on(
event: string,
listener: (...args: any[]) => void,
): this
on(
event: "close",
listener: () => void,
): this
on(
event: "connection",
listener: (socket: Socket) => void,
): this
on(
event: "error",
listener: (err: Error) => void,
): this
on(
event: "listening",
listener: () => void,
): this
on(
event: "drop",
listener: (data?: DropArgument) => void,
): this
once(
event: string,
listener: (...args: any[]) => void,
): this
once(
event: "close",
listener: () => void,
): this
once(
event: "connection",
listener: (socket: Socket) => void,
): this
once(
event: "error",
listener: (err: Error) => void,
): this
once(
event: "listening",
listener: () => void,
): this
once(
event: "drop",
listener: (data?: DropArgument) => void,
): this
prependListener(
event: string,
listener: (...args: any[]) => void,
): this
prependListener(
event: "close",
listener: () => void,
): this
prependListener(
event: "connection",
listener: (socket: Socket) => void,
): this
prependListener(
event: "error",
listener: (err: Error) => void,
): this
prependListener(
event: "listening",
listener: () => void,
): this
prependListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this
prependOnceListener(
event: string,
listener: (...args: any[]) => void,
): this
prependOnceListener(
event: "close",
listener: () => void,
): this
prependOnceListener(
event: "connection",
listener: (socket: Socket) => void,
): this
prependOnceListener(
event: "error",
listener: (err: Error) => void,
): this
prependOnceListener(
event: "listening",
listener: () => void,
): this
prependOnceListener(
event: "drop",
listener: (data?: DropArgument) => void,
): this
ref(): this
Opposite of `unref()`, calling `ref()` on a previously `unref`ed server will _not_ let the program exit if it's the only server left (the default behavior). If the server is `ref`ed calling `ref()` again will have no effect.
unref(): this
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 `unref`ed calling`unref()` again will have no effect.