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. Everyhttp.serve needs --allow-net on the
bind address:
- The server always binds
0.0.0.0, and the permission check is always0.0.0.0:<port>— even when clients connect over loopback. Granting127.0.0.1:3000orlocalhost:3000will 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.keyfile paths need--allow-read; inline PEM does not.FF_CERT/FF_KEYused byff startare CLI reads and need no runtime grant.FF_ECHOis runtime config and needs no--allow-env, but echo mode does not bypass the listener’s--allow-netcheck.- Denial is synchronous:
Hello-world server
Createserver.js:
ff:
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 to3000:
Port reuse (reusePort)
To allow multiple server processes to bind to the same port using kernel load balancing (SO_REUSEPORT), set reusePort: true:
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-standardHeadersinstance (populated for both HTTP/1.1 and HTTP/2)
Working with request headers
Theheaders parameter provides standard Headers methods:
headers.get(name): returns the header value (case-insensitive), ornullif absent. Multi-value headers are comma-joined ("a, b"), exceptCookiewhich joins with"; "per RFC 9113 §8.2.3, andSet-Cookiewhich returns the first value (usegetAllto retrieve all).headers.has(name): returnstrueif present,falseotherwise.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 receive431 Request Header Fields Too Large.
Responding with real data
Return aResponse with the status, headers, and body you want:
Routing requests
Matchurl (and method) to serve more than one endpoint:
ff imprint routing package.
Practical example: JSON REST API
A tiny notes API backed by SQLite, zero dependencies. Schema statements belong indb.execNoArgs (multi-statement, no params); single statements
with bound params use db.exec:
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
SetFF_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:
--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
- Permissions — flags, scopes, denial contract
- http API — full
http.servereference - TLS guide — HTTPS/WSS setup
- Limitations — every hard cap