HttpServer
Defined in: packages/net-http/src/server/http-server.ts:93
Serves HTTP on any number of listeners, with one handler for every request whatever version carried it.
The protocol of each accepted connection comes from ALPN: http/1.1, or none
on a cleartext connection, is served as HTTP/1.x; h2 as HTTP/2 when the
http2 option is set. With it, a cleartext connection is served as HTTP/2
when it opens with the HTTP/2 connection preface. A connection that
negotiated anything else is closed and reported as an error.
A TLS listener offers the protocols it lists in its alpnProtocols option;
list h2 there to have HTTP/2 negotiated. A QUIC listener’s connections are
served as HTTP/3 when they negotiated h3 and the http3 option is set.
Example
Section titled “Example” const server = new HttpServer({ handler: () => ({ status: 200, body: new TextEncoder().encode("hi") }), maxHeadSize: 16 * 1024, idleTimeoutMs: 5000, maxConnections: 100, }); server.listen(await TcpListener.listen({ host: "127.0.0.1", port: 8080 })); ```;
## Extends
- `EventDispatcherMapBase`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\>
## Constructors
### Constructor
> **new HttpServer**(`options`): `HttpServer`
Defined in: packages/net-http/src/server/http-server.ts:102
#### Parameters
##### options
[`HttpServerOptions`](../type-aliases/HttpServerOptions.md)
#### Returns
`HttpServer`
#### Overrides
`EventDispatcherMapBase<HttpServerEvents>.constructor`
## Accessors
### connectionCount
#### Get Signature
> **get** **connectionCount**(): `number`
Defined in: packages/net-http/src/server/http-server.ts:107
Connections currently served.
##### Returns
`number`
## Methods
### \[asyncDispose\]()
> **\[asyncDispose\]**(): `Promise`\<`void`\>
Defined in: packages/net-http/src/server/http-server.ts:297
Closes the server, so it can be used with `await using`.
#### Returns
`Promise`\<`void`\>
***
### close()
> **close**(`signal?`): `Promise`\<`void`\>
Defined in: packages/net-http/src/server/http-server.ts:262
Stop accepting, let every connection finish the request it is handling,then close it.
#### Parameters
##### signal?
`AbortSignal`
Aborting it destroys the connections still open, aborting their handlers' signals.
#### Returns
`Promise`\<`void`\>
Resolves once every connection closed.
***
### isSubscribed()
> **isSubscribed**\<`K`\>(`event`, `listener`): `boolean`
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:38
Check if a callback is subscribed to a specific event.
#### Type Parameters
##### K
`K` *extends* `"error"`
#### Parameters
##### event
`K`
The event name.
##### listener
`Callable`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
The callback to check.
#### Returns
`boolean`
#### Inherited from
`EventDispatcherMapBase.isSubscribed`
***
### listen()
> **listen**(`listener`): `void`
Defined in: packages/net-http/src/server/http-server.ts:118
Serve every connection `listener` accepts: byte-stream connections (TCP,TLS) or multiplexed ones (QUIC). The server takes ownership: [close](#close)closes the listener.
#### Parameters
##### listener
`IListener`\<`TransportConnection` \| `IMultiplexedConnection`\>
#### Returns
`void`
#### Throws
When the server is closing.
***
### serve()
> **serve**(`connection`): `void`
Defined in: packages/net-http/src/server/http-server.ts:133
Serve one connection accepted elsewhere.
#### Parameters
##### connection
`TransportConnection` \| `IMultiplexedConnection`
#### Returns
`void`
***
### subscribe()
> **subscribe**\<`K`\>(`event`, `listener`, `options?`): `Callable` \| `null`
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:23
#### Type Parameters
##### K
`K` *extends* `"error"`
#### Parameters
##### event
`K`
##### listener
`Callable`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
##### options?
`EventDispatcherSubscribeOptions`
#### Returns
`Callable` \| `null`
#### Inherited from
`EventDispatcherMapBase.subscribe`
***
### unsubscribe()
> **unsubscribe**\<`K`\>(`event`, `listener`): `void`
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:31
Unsubscribe a callback from a specific event.
#### Type Parameters
##### K
`K` *extends* `"error"`
#### Parameters
##### event
`K`
The event name.
##### listener
`Callable`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
The callback to remove.
#### Returns
`void`
#### Inherited from
`EventDispatcherMapBase.unsubscribe`
***
### wait()
> **wait**\<`K`\>(`event`, `options?`): `Promise`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
Defined in: packages/async/src/events/event-dispatcher-map-base.ts:45
Wait for the next occurrence of a specific event, optionally matching apredicate.
This is a convenience method that subscribes to the dispatcher, waits forthe next event that matches the predicate (if provided), and thenunsubscribes automatically.
#### Type Parameters
##### K
`K` *extends* `"error"`
#### Parameters
##### event
`K`
The event name to wait for.
##### options?
`EventDispatcherWaitOptions`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
Optional settings including predicate and abort signal.
#### Returns
`Promise`\<[`HttpServerEvents`](../type-aliases/HttpServerEvents.md)\[`K`\]\>
A promise that resolves with the event arguments as an array.
#### Inherited from
`EventDispatcherMapBase.wait`