serve (@shadow-js/dusk/node)
Serves a handler over HTTP with Node's http module.
Serves a handler over HTTP with Node's http module.
Requests become Web Requests with a streaming body and an abort signal that fires when the
client disconnects; responses stream back with backpressure. Static files (the client build)
are served first, with safe path resolution, validators, ranges and compression. Shutdown on
SIGTERM / SIGINT is graceful: no new connections, requests in flight finish, then exit.
One client cannot hold the process open: sockets are capped (maxConnections), headers and the whole request
have deadlines (headersTimeoutMs, requestTimeoutMs), an idle keep-alive socket is closed
(keepAliveTimeoutMs) after a bounded number of requests (maxRequestsPerSocket), and a client that stops
reading its response is dropped (writeTimeoutMs). Each has a default; 0 turns one off.
It reads no .env file. The server code sees process.env as the platform set it (the dev
server and the build's prerender step load the env files; production does not). Start with
node --env-file=<file> to keep a file next to the server.
With workers above 1 the process becomes the primary of a worker pool (see ServeOptions.workers) and the handle's
role is "primary". Without workers nothing of the pool is loaded.
Import#
import { serve } from "@shadow-js/dusk/node";
Signature#
async function serve(handler: Handler, options: ServeOptions = {}): Promise<ServerHandle>
Parameters#
| Name | Description |
|---|---|
handler |
The fetch handler, usually from createHandler. |
options |
Port, host, static files, compression, shutdown and workers. |
Returns#
A running server (or, in the primary of a worker pool, the pool's handle).
Throws#
- RangeError for an invalid
workers.
Examples#
import { createHandler } from "@shadow-js/dusk/server";
import { serve } from "@shadow-js/dusk/node";
await serve(createHandler({ manifest, features: [pages()] }), { port: 3000, host: "0.0.0.0", static: { dir: "dist/client" } });