Server entry exports and pipeline
hamur build writes the server as one module, .output/server/index.mjs (the path of each preset is on its page). What that module exports is the contract between your app and the host that runs it.
What each preset exports
| Preset | Exports of the entry |
|---|---|
bun, bun-compile, node, iis-node, iis-handler, firebase-app-hosting and the host aliases of node | the default export, and fetch, listen and renderPage by name |
node-middleware | handler, fetch, close and middleware |
standard, zephyr | export default { fetch(request, ctx?) } |
cloudflare | a default export with fetch, scheduled, queue, email and tail, and the names of defineServer exports, such as a Durable Object class |
cloudflare-pages | _worker.js/index.js with fetch |
aws-lambda, lambda-edge, stormkit | handler |
A name that the entry already exports (default, fetch, port, websocket, handler, close, listen, renderPage, middleware, scheduled, queue, email, tail, HamurWebSocket) cannot be used in exports; the build fails with server/index.ts: exports.fetch collides with an entry export of the built server; rename it. exports is for what the host imports by name, see The one door.
From your own code
On bun, bun-compile, node and the presets that run node's entry (iis-node, iis-handler, firebase-app-hosting and the host aliases), the entry also exports three functions by name, beside its default export:
.output/server/index.mjs export { fetch, listen, renderPage }
Importing the entry starts no server: it listens on import only when it is the script the process started (bun .output/server/index.mjs, node .output/server/index.mjs, the Docker CMD, hamur preview), checked by comparing the started script with the entry after both are resolved through symlinks. node --import ./setup.mjs .output/server/index.mjs still listens, since --import does not change the started script. Importing the entry does run the server plugins and tasks.runOnStart, once, as starting it does.
fetch(request) is the handler the server runs, with the same compression and waitUntil. On bun, mounted as Bun.serve({ fetch }), it takes Bun's server as its second argument, so the client address (and with it ipRestriction), WebSocket upgrades, publish and the SSE timeout work as on the entry's own server. The first part of the sample below mounts it in a Bun.serve of your own.
listen({ port?, host? }) starts the preset's server (Bun.serve, or node:http) on port and host, by default PORT (else 3000) and HOST, with server.https when it is set, and resolves { url, close() }. port: 0 takes a free port, which url names. close() stops accepting, gives requests in flight up to 10 seconds, closes the connections left and resolves; it does not run the close hook or end the process. listen installs no signal handler and starts no scheduledTasks and no server.pubsub: your code owns the process. On node it starts one server in the calling process, whatever node.cluster says.
interface ServerEntry {
fetch(request: Request): Promise<Response>
listen(options?: { port?: number; host?: string }): Promise<{ url: string; close(): Promise<void> }>
}
const entry = "./.output/server/index.mjs"
const { fetch: handle, listen } = (await import(entry)) as ServerEntry
export const server = Bun.serve({ port: 8080, fetch: handle })
export async function smokeTest(): Promise<number> {
const app = await listen({ port: 0, host: "127.0.0.1" })
const { status } = await fetch(app.url)
await app.close()
return status
}
renderPage has its own page.
Your own pipeline
A request runs a pipeline of stages: public files, route rules, the middleware list, handler routes, pages and the 404. defineServer({ fetch }) replaces it with one composed from the stages of hamur/pipeline and any of your own, and the preset wraps that fetch instead of the default pipeline. The hooks, the request id, tracing, Server-Timing, the security headers, the masking of errors, and the ipRestriction and basicAuth rules run around any fetch.
The stages, the warnings for a stage you leave out, defaultPipeline and the samples are in Your own pipeline and the API reference.