Skip to content

@edgetime/node

Serves a Fetch API handler on Node's HTTP server, with streaming, graceful shutdown and safe defaults.

npm install @edgetime/node

Runs 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.

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.

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);
});
  • 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-Length instead of chunked encoding.
  • The request’s signal aborts 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 to onError.
  • Repeated Set-Cookie headers are sent as separate headers.
  • The host in request.url comes from origin, or else from :authority on HTTP/2 and the Host header on HTTP/1.1. The request target cannot change it. A host that is malformed, sent twice, or that URL parsing would rewrite (such as 0x7f000001), is answered with 400. TRACE, which the Fetch API cannot represent, is answered with 405.
  • 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 Host header is not passed to the handler; the host is :authority.
  • A response with a non-zero Content-Length and no body fails, except for HEAD and 304.
  • A HEAD response is sent without a body, and the body’s source is cancelled.
  • Connection, Keep-Alive, Transfer-Encoding, Upgrade and Proxy-Connection response headers are not sent.
  • A response from fetch() is sent without its upstream Content-Encoding and Content-Length when Node has already decoded the body (gzip, deflate, br, zstd). This also holds after withHeaders() or edgetime middleware has copied the response. If you copy it yourself with new 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 500 with 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.

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.