Skip to main content
The http module starts servers. It has one function, http.serve, plus the response helpers you return from your handler.

Permissions

http.serve listens on the network, so it always needs --allow-net on the bind address:
  • The server binds to 0.0.0.0; there is no host option. The check is always 0.0.0.0:<port>, even when clients connect over loopback. Granting 127.0.0.1:3000 or localhost:3000 will not let the server start.
  • A scope without a port matches every port: --allow-net=0.0.0.0 is enough for any listener port.
  • The same grant covers HTTP, WebSocket upgrades, and TLS on that port.
  • A denied listener throws synchronously from http.serve:

TLS files need read

tls.cert and tls.key are either inline PEM (the string starts with -----BEGIN ) or a file path. Inline PEM needs no file permission; a path needs --allow-read on that file:
A denied TLS path throws PermissionDenied with messages like http.serve tls.cert requires --allow-read for 'cert.pem' (and the same for tls.key) before the listener starts. Path scopes follow the normal rules: resolved against the launch cwd, symlink-checked, and not moved by process.chdir(). The FF_CERT / FF_KEY env vars used by ff start are read by the CLI and do not need --allow-read. Only paths passed to http.serve({ tls }) are runtime reads. Full model: Permissions.

Quick look

http.serve(signatures)

Two call shapes are accepted, and both return a server handle ({ port, close }):
The server binds to 0.0.0.0. There is no host option. When port: 0 is passed, srv.port contains the actual bound port assigned by the OS.
The one-argument form http.serve(handler) is not supported by the current runtime — it throws TypeError: http.serve requires (options, handler). Use http.serve(3000, handler) or http.serve({}, handler) for the default port.

Options reference

Graceful shutdown: server.close() and signals

server.close(callback?) stops accepting new connections and drains existing ones cleanly:
  • Closes the listening socket immediately so new connections are refused.
  • Closes idle HTTP/1.1 keep-alive sockets and active WebSocket connections.
  • Marks in-flight HTTP/1.1 requests with Connection: close so each socket closes as soon as its response finishes flushing.
  • Sends HTTP/2 GOAWAY on active H2 sessions and closes them once active streams complete.
  • Resolves the returned Promise<void> (and invokes optional callback) once all 512 connection slots are free, allowing the event loop to exit naturally.
If you do not register your own SIGINT or SIGTERM listener via process.on / process.once, http.serve automatically traps SIGINT and SIGTERM and calls server.close() for you. When the websocket block is present, any request carrying Connection: Upgrade, Upgrade: websocket, and Sec-WebSocket-Key is upgraded before your handler runs — the handler never sees that request, and no { status: 101 } branch is needed (a handler that would return 404 still gets the 101). The handler serves plain HTTP only:
WebSocket upgrades share the listener’s net grant: --allow-net=0.0.0.0:3000. Server sockets expose send / sendBinary only — track peers by object reference in a Set (see the WebSocket guide).

Handler reference

The handler is called as (url, method, body, headers): Return one of: Async handlers work — the 30s watchdog still applies:
An async handler that calls out needs grants for both sides:
The listener is granted by 0.0.0.0:3000; the outbound fetch is granted by example.com:443. One repeatable flag can carry both scopes.

Response helpers

Build responses with the global Response class:
Read bodies back with text() / json() / arrayBuffer() / bytes() / blob() / formData() — all work on Request and Response. Bodies can be re-read freely (bodyUsed stays false; bytes() returns ArrayBuffer). Headers are case-insensitive and lowercased internally:

Full example

Runtime config: FF_ECHO

FF_ECHO is read by the runtime, not by JavaScript, so it needs no --allow-env. Setting any value (even empty) makes every request return 200 {"message":"ok"} without invoking your handler:
The listener still needs its net grant — echo mode bypasses the handler, not the permission check. See the env guide.

Limits (summary)

Full table lives in the Limitations reference; the ones that bite here: 512 concurrent connections, 4 KB max HTTP/1.1 request headers (→ 431), 1 MB max HTTP/1.1 request body (4 KB static buffer spills to the heap, 64 MB global spill budget; → 413 / 503; HTTP/2 allows 10 MB per stream / 16 MB per session), 10 MB max response body (the 64 KB staging buffer spills to the heap), 30s read deadline (incomplete requests terminated), 30s handler watchdog (→ 504), strict HTTP framing (ambiguous Content-Length/Transfer-Encoding, obs-fold, or BWS rejected with 400 Bad Request), Response status clamped to 100..999, plain listeners speak HTTP/1.1 (HTTP/2 needs TLS), bind is always 0.0.0.0.

See also