Skip to main content
http.serve starts an HTTP server bound to 0.0.0.0. Your handler is called per request as (url, method, body, headers) and returns a Response.

Permissions

Servers are deny-by-default. Every http.serve needs --allow-net on the bind address:
  • The server always binds 0.0.0.0, and the permission 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 start the listener.
  • A scope without a port (--allow-net=0.0.0.0) covers every port.
  • The same grant covers HTTP, WebSocket upgrades, and TLS on that port.
  • tls.cert / tls.key file paths need --allow-read; inline PEM does not. FF_CERT / FF_KEY used by ff start are CLI reads and need no runtime grant.
  • FF_ECHO is runtime config and needs no --allow-env, but echo mode does not bypass the listener’s --allow-net check.
  • Denial is synchronous:
Full model: Permissions.

Hello-world server

Create server.js:
Run it with ff:
Verify in another terminal:
The handler can also be async (or return a Promise) — the connection parks until it settles:

Listening on a specific port

Pass the port in the options object, or use the shorthand form. When omitted, the port defaults to 3000:

Port reuse (reusePort)

To allow multiple server processes to bind to the same port using kernel load balancing (SO_REUSEPORT), set reusePort: true:
By default reusePort is false (safe standard SO_REUSEADDR is always enabled). Note that on Linux all processes sharing a port must set reusePort: true.
The server always binds to 0.0.0.0. There is no host option, no --port flag, and no FF_PORT environment variable — the port comes only from your JS. The permission scope must name that bind address.
http.serve(handler) (one argument) is not supported by the current runtime; it throws TypeError: http.serve requires (options, handler). Use http.serve(3000, handler) or http.serve({}, handler) instead.

Inspecting the incoming request

The handler receives (url, method, body, headers):
  • url: request path + query string (string)
  • method: uppercase HTTP method (e.g. "GET", string)
  • body: request text (string, empty when none)
  • headers: web-standard Headers instance (populated for both HTTP/1.1 and HTTP/2)

Working with request headers

The headers parameter provides standard Headers methods:
  • headers.get(name): returns the header value (case-insensitive), or null if absent. Multi-value headers are comma-joined ("a, b"), except Cookie which joins with "; " per RFC 9113 §8.2.3, and Set-Cookie which returns the first value (use getAll to retrieve all).
  • headers.has(name): returns true if present, false otherwise.
  • headers.getAll(name): returns an array of all values for the header name.
  • Total request header size is capped at 4 KB for HTTP/1.1 (READ_BUF_SIZE) and 64 KiB for HTTP/2. Requests exceeding the limit receive 431 Request Header Fields Too Large.
body is always a string. For JSON, parse it yourself with JSON.parse(body || "{}") — and guard the parse, since a client can send anything.

Responding with real data

Return a Response with the status, headers, and body you want:
Three return shapes are accepted:
Anything else — including a bare string — produces an empty 200 response. return "hello" does not send "hello". Always wrap it: return new Response("hello").

Routing requests

Match url (and method) to serve more than one endpoint:
For larger route trees, put the matching in a small router function or reach for an ff imprint routing package.

Practical example: JSON REST API

A tiny notes API backed by SQLite, zero dependencies. Schema statements belong in db.execNoArgs (multi-statement, no params); single statements with bound params use db.exec:
SQLite opens the database read/write and writes WAL siblings next to it; the file needs both grants and the parent directory needs write.
db.lastInsertRowId() is a method — the () is required. Without it you’d serialize the function object instead of the id.

Smoke-testing with FF_ECHO

Set FF_ECHO to any value and every request returns canned 200 {"message":"ok"} without running your handler. Use it to verify the port mapping and deployment before debugging app logic:
Echo mode is runtime configuration, so it needs no --allow-env. It still needs the listener grant.

Reference: limits

Troubleshooting

http.serve throws PermissionDenied — the listener’s port is outside every --allow-net scope. Add --allow-net=0.0.0.0:<port>. 127.0.0.1 and localhost will not match the bind address. TLS fails with PermissionDenied on tls.cert / tls.key — the path is outside --allow-read. Add the file (and any symlink target) to the scope, or pass inline PEM, which needs no file grant. Empty 200 when I return a string — wrap it: return new Response("hello"). Bare strings are not bodies. Connection refused — confirm the port in http.serve({ port }) matches your curl command or Docker -p mapping. No environment variable overrides it. 504 on slow endpoints — the 30s handler watchdog fired. Move heavy work to a Worker or reply early and finish in the background. id comes back wrong in JSON — you wrote db.lastInsertRowId without calling it. Always db.lastInsertRowId(). Stopping the server — http.serve returns { port, close }. Call await server.close() (or pass { signal } to http.serve) to stop accepting new connections and drain active requests cleanly. SIGINT and SIGTERM also trigger server.close() automatically when no custom listener is registered for that signal.

See also