@edgetime/node
Serves a Fetch API handler on Node's HTTP server, with streaming, graceful shutdown and safe defaults.
npm install @edgetime/nodeRuns a Fetch API handler on Node’s HTTP server. Deno, Bun and Cloudflare Workers serve fetch handlers natively and do not need an adapter. Requires Node 24.13 or later.
import { serve } from '@edgetime/node';import { app } from './app.ts';
const server = await serve(app.fetch, { port: 3000, env: { DATABASE_URL: process.env.DATABASE_URL }, onError: (error) => console.error(error),});The handler receives (request, env, ctx). env is the value you pass in the options, and
ctx.waitUntil(promise) lets work continue after the response; a rejection goes to onError.
Options
Section titled “Options”| Option | Default | Purpose |
|---|---|---|
env |
required | Passed to the handler: configuration and the dependencies it needs. |
port |
3000 |
Use 0 for any free port. |
hostname |
all interfaces | The address to listen on. |
origin |
from the connection and Host |
The public origin, such as https://api.example.com. Set it behind a proxy that terminates TLS, so request.url is correct. Anything but an http(s) origin without a path throws at start-up. |
onError |
none | Called when the handler throws, a response cannot be sent or background work fails. It may be async; if it throws or rejects, the server carries on. |
requestTimeout |
Node’s 300 000 | Milliseconds to receive a whole request; 0 turns it off. |
headersTimeout |
Node’s 60 000 | Milliseconds to receive the request headers. |
keepAliveTimeout |
Node’s 5 000 | Milliseconds an idle keep-alive connection stays open. |
maxHeaderSize |
Node’s 16 KiB | Largest request header block, from 1 KiB to 1 MiB. |
The timeouts are whole milliseconds up to a day; any other value throws at start-up.
Shutting down
Section titled “Shutting down”serve() resolves to the Server with one more method. server.shutdown({ timeout? }) stops
accepting connections, closes idle keep-alive connections, and lets requests in progress and their
waitUntil work finish. While it waits, each HTTP/1 response is sent with Connection: close.
After timeout milliseconds (10 000 by default) the remaining connections are closed anyway. It
resolves to true when everything finished in time.
process.once('SIGTERM', async () => { const drained = await server.shutdown({ timeout: 10_000 }); process.exit(drained ? 0 : 1);});Behaviour
Section titled “Behaviour”- Request and response bodies are streamed, with backpressure. Each response chunk is written as
soon as it is produced, so server-sent events are not delayed. A body that is already complete
in its first chunk is sent with a
Content-Lengthinstead of chunked encoding. - The request’s
signalaborts when the client disconnects before the response is complete, so a handler can pass it to upstream calls. A client that leaves mid-response is not reported toonError. - Repeated
Set-Cookieheaders are sent as separate headers. - The host in
request.urlcomes fromorigin, or else from:authorityon HTTP/2 and theHostheader on HTTP/1.1. The request target cannot change it. A host that is malformed, sent twice, or that URL parsing would rewrite (such as0x7f000001), is answered with400.TRACE, which the Fetch API cannot represent, is answered with405. - A request body the handler did not finish reading is read and dropped after the response, up to 1 MiB, so the connection can carry the next request. A larger remainder closes the connection. On HTTP/2 the stream is closed as soon as the response is sent, which tells the client to stop uploading and leaves the connection’s other streams alone.
- On HTTP/2 a
Hostheader is not passed to the handler; the host is:authority. - A response with a non-zero
Content-Lengthand no body fails, except forHEADand304. - A
HEADresponse is sent without a body, and the body’s source is cancelled. Connection,Keep-Alive,Transfer-Encoding,UpgradeandProxy-Connectionresponse headers are not sent.- A response from
fetch()is sent without its upstreamContent-EncodingandContent-Lengthwhen Node has already decoded the body (gzip, deflate, br, zstd). This also holds afterwithHeaders()or edgetime middleware has copied the response. If you copy it yourself withnew Response(upstream.body, upstream), delete those two headers. - A streamed body is counted against its
Content-Length. If the two disagree the response fails and the connection is closed, so the extra bytes are never sent. - When a response cannot be sent, the client gets a plain
500with none of that response’s headers. - A handler that throws is answered with a plain
500. A router never throws, so this covers bare handlers.
Other servers
Section titled “Other servers”createListener(handler, { env, origin?, onError? }) returns a (request, response) listener for
node:https or the HTTP/2 compatibility API. Its drain(timeout?) method does the waiting part
of a shutdown for a server you manage: call it after server.close(), and it resolves to true
once no request or waitUntil work is left, or false after timeout milliseconds. toRequest(incoming, origin?) and
sendResponse(response, outgoing) are the two conversions on their own.
import { createServer } from 'node:https';import { createListener } from '@edgetime/node';
createServer({ key, cert }, createListener(app.fetch, { env })).listen(443);Source: packages/node on GitHub.