Server - Node documentation
class Server
extends NetServer

Usage in Deno

import { Server } from "node:http";

Constructors

new
Server(requestListener?: RequestListener<Request, Response>)
new
Server()

Type Parameters

Response extends ServerResponse = ServerResponse

Properties

Limit the amount of time the parser will wait to receive the complete HTTP headers.

If the timeout expires, the server responds with status 408 without forwarding the request to the request listener and then closes the connection.

It must be set to a non-zero value (e.g. 120 seconds) to protect against potential Denial-of-Service attacks in case the server is deployed without a reverse proxy in front.

The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e.,server.timeout.

A value of 0 will disable the keep-alive timeout behavior on incoming connections. A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.

The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections.

maxHeadersCount: number | null

Limits maximum incoming headers count. If set to 0, no limit will be applied.

maxRequestsPerSocket: number | null

The maximum number of requests socket can handle before closing keep alive connection.

A value of 0 will disable the limit.

When the limit is reached it will set the Connection header value to close, but will not actually close the connection, subsequent requests sent after the limit is reached will get 503 Service Unavailable as a response.

Sets the timeout value in milliseconds for receiving the entire request from the client.

If the timeout expires, the server responds with status 408 without forwarding the request to the request listener and then closes the connection.

It must be set to a non-zero value (e.g. 120 seconds) to protect against potential Denial-of-Service attacks in case the server is deployed without a reverse proxy in front.

timeout: number

The number of milliseconds of inactivity before a socket is presumed to have timed out.

A value of 0 will disable the timeout behavior on incoming connections.

The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections.

Methods

addListener(
event: string,
listener: (...args: any[]) => void,
): this
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: "checkContinue",
): this
addListener(
event: "checkExpectation",
): this
addListener(
event: "clientError",
listener: (
err: Error,
socket: stream.Duplex,
) => void
,
): this
addListener(
event: "connect",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
addListener(
event: "dropRequest",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
) => void
,
): this
addListener(
event: "request",
): this
addListener(
event: "upgrade",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this

Closes all connections connected to this server.

Closes all connections connected to this server which are not sending a request or waiting for a response.

emit(
event: string,
...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: "checkContinue",
req: InstanceType<Request>,
res: InstanceType<Response> & { req: InstanceType<Request>; },
): boolean
emit(
event: "checkExpectation",
req: InstanceType<Request>,
res: InstanceType<Response> & { req: InstanceType<Request>; },
): boolean
emit(
event: "clientError",
err: Error,
socket: stream.Duplex,
): boolean
emit(
event: "connect",
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
): boolean
emit(
event: "dropRequest",
req: InstanceType<Request>,
socket: stream.Duplex,
): boolean
emit(
event: "request",
req: InstanceType<Request>,
res: InstanceType<Response> & { req: InstanceType<Request>; },
): boolean
emit(
event: "upgrade",
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
): boolean
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: "checkContinue",
): this
on(
event: "checkExpectation",
): this
on(
event: "clientError",
listener: (
err: Error,
socket: stream.Duplex,
) => void
,
): this
on(
event: "connect",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
on(
event: "dropRequest",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
) => void
,
): this
on(
event: "request",
): this
on(
event: "upgrade",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => 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: "checkContinue",
): this
once(
event: "checkExpectation",
): this
once(
event: "clientError",
listener: (
err: Error,
socket: stream.Duplex,
) => void
,
): this
once(
event: "connect",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
once(
event: "dropRequest",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
) => void
,
): this
once(
event: "request",
): this
once(
event: "upgrade",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => 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: "checkContinue",
): this
prependListener(
event: "checkExpectation",
): this
prependListener(
event: "clientError",
listener: (
err: Error,
socket: stream.Duplex,
) => void
,
): this
prependListener(
event: "connect",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
prependListener(
event: "dropRequest",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
) => void
,
): this
prependListener(
event: "request",
): this
prependListener(
event: "upgrade",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => 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: "checkContinue",
): this
prependOnceListener(
event: "checkExpectation",
): this
prependOnceListener(
event: "clientError",
listener: (
err: Error,
socket: stream.Duplex,
) => void
,
): this
prependOnceListener(
event: "connect",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
prependOnceListener(
event: "dropRequest",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
) => void
,
): this
prependOnceListener(
event: "request",
): this
prependOnceListener(
event: "upgrade",
listener: (
req: InstanceType<Request>,
socket: stream.Duplex,
head: Buffer,
) => void
,
): this
setTimeout(
msecs?: number,
callback?: () => void,
): this

Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as an argument, if a timeout occurs.

If there is a 'timeout' event listener on the Server object, then it will be called with the timed-out socket as an argument.

By default, the Server does not timeout sockets. However, if a callback is assigned to the Server's 'timeout' event, timeouts must be handled explicitly.

setTimeout(callback: () => void): this