# Node

Needs Node 24 (LTS) or newer. The entry uses `node:http` and serves `public/` itself. `node .output/server/index.mjs` listens on `PORT` (default 3000) and `HOST`, and drains as described under [Bun](/deploy/presets/bun#shutdown). It runs [WebSockets](/server#websockets) on `node:http`'s `upgrade` event with framing Hamur writes itself, so no package is added; at shutdown each open socket gets a close frame with code 1001.

## Reference

|                      |                        |
| -------------------- | ---------------------- |
| Preset               | `node`                 |
| Runs on              | Node 24 (LTS) or newer |
| `event.runtime.name` | `node`                 |
| Server target        | `node`                 |
| Lifetime, startup    | `process`, `load`      |

### Output

```text
.output/
  hamur.json
  public/                    client assets and public/ files, with .gz, .br and .zst copies when build.compressPublic is on
  server/
    index.mjs                the server entry
    public-assets.json       size and strong ETag of every public file
    node_modules/            traced externals, with package.json (only when the build traced any)
```

### Config keys

| Key                         | Default               | What it does                                                                                               |
| --------------------------- | --------------------- | ---------------------------------------------------------------------------------------------------------- |
| `server.https`              | unset                 | `{ key, cert, ca?, passphrase? }` makes the server serve TLS; env `HAMUR_SSL_KEY` and `HAMUR_SSL_CERT` win |
| `server.unix`               | unset                 | listen on this Unix socket instead of `PORT` and `HOST`; env `HAMUR_UNIX_SOCKET` wins                      |
| `server.maxRequestBodySize` | `134217728` (128 MiB) | the largest request body the server accepts                                                                |
| `build.compressPublic`      | `false`               | write `.br`, `.zst` and `.gz` copies of text, JSON, SVG, font and WASM files of 1 KiB or more              |
| `node.requestTimeout`       | `300000`              | milliseconds a request may take to arrive; `0` is no timeout                                               |
| `node.headersTimeout`       | `66000`               | milliseconds for the request headers, never above `requestTimeout`                                         |
| `node.maxConnections`       | no cap                | a positive integer                                                                                         |
| `node.cluster`              | unset                 | workers forked with `node:cluster`, a positive integer or `"auto"`; env `HAMUR_CLUSTER_WORKERS` wins       |

### Detection

None. Name it with `--preset`, `preset` in the config or `HAMUR_PRESET`; no environment variable selects it.

### Deploy command

No `hamur deploy` command: it exits 1 for this preset. Run `node .output/server/index.mjs` on the host, or build an image with [`hamur build --docker`](/deploy/platforms/docker). The preview command of the preset, which `hamur preview` runs or prints, is `node .output/server/index.mjs`.

### Limits

- Runs: WebSockets and `server.syncedState`, streamed answers, `server.metrics`, private `app/env.ts` entries read at run time.
- Does not run: `server.pubsub`, image transforms at run time, Durable Object classes.
- `scheduledTasks` run in process in UTC, `tasks.runOnStart` runs, `tasks.queue` does not apply. The request body limit is `server.maxRequestBodySize`, 128 MiB.

The levels are the `features` of the preset: `hamur build` warns or fails when the app uses a feature the preset does not run (see [Preset features](/deploy#preset-features)).

````sh [deploy-node.sh]
hamur build --preset node
PORT=8080 HOST=0.0.0.0 node .output/server/index.mjs
````

## Cluster

`node: { cluster: 4 }` in the config, or `HAMUR_CLUSTER_WORKERS=4` at start (the variable wins), makes `node .output/server/index.mjs` a primary process that forks that many workers with `node:cluster`; `"auto"` forks one per CPU (`os.availableParallelism()`). The workers share `PORT` and each runs the whole server. A value that is not a positive integer or `"auto"` stops the build (`node.cluster must be a positive integer or "auto" (got 0)`) or the start (`HAMUR_CLUSTER_WORKERS must be a positive integer or "auto" (got "0")`).

A worker that exits is replaced after 100 ms, then 200 ms, doubling up to 10 seconds for each further exit in its place; a worker that ran for 60 seconds resets its place to 100 ms. `scheduledTasks` run in the primary, once for the cluster; `tasks.runOnStart` runs once, in the primary; server plugins run in the primary and in each worker, since each process has its own app. On `SIGTERM` or `SIGINT` the primary starts no new worker, tells every worker to drain as described under [Bun](/deploy/presets/bun#shutdown), runs its own `close` hook and exits with code 0 once all of them have exited, or 1 when one did not drain. Memory storage and in-process caches are per worker, which the build log says: keep shared state in a storage driver such as `fs` or `redis`. `node.cluster` is read by the `node` preset only.

## Compression

The bun and node servers compress dynamic answers (`server.compress`, on by default); on the other presets the host does it. For each request the first coding of `encodings` (default `["zstd", "br", "gzip"]`; `"deflate"` is the fourth) that its `Accept-Encoding` accepts is used, and an answer of a compressible type (text, JSON, JavaScript, XML, SVG, fonts and `+json` or `+xml` types) whose length is known and is `threshold` bytes or more (default 1024), or a streamed one, is sent with `Content-Encoding`, without `Content-Length`, and with `Vary: Accept-Encoding`. A streamed answer stays streamed: each chunk is flushed compressed as it is written.

An answer is left as it is when it already has a `Content-Encoding`, its `Cache-Control` has `no-transform`, it is `text/event-stream`, a `Range` request asked for it or its status is 206, 204 or 304, the request is `HEAD`, or its known length is smaller than `threshold`. A strong `ETag` of a compressed answer gets `-<coding>` (`"abc-br"`), a weak one is kept, and `If-None-Match` with the compressed form still matches: the 304 carries it. `server.compress: false` turns it off.

## Early hints

`server.earlyHints: true` makes a `GET` page send its stylesheets and module chunks to the browser before the page is rendered. The node server writes a `103 Early Hints` after the route's middleware and before its loaders run; the final answer of every preset carries the same `Link` header (`</_hamur/app.css>; rel=preload; as=style`, `</_hamur/entry.js>; rel=modulepreload; crossorigin`), which Cloudflare turns into a `103` itself. On bun, vercel and netlify the header is all that is sent. The value is capped at 8 KiB and later links are dropped. A page with `scripts: false` lists stylesheets only.

Nothing is added to a non-HTML answer, a page served from the cache (`cache`, `swr`, ISR), or an answer marked `no-store`. A handler or middleware can send its own hints with [`writeEarlyHints`](/api-reference/server#writeearlyhints-earlyhintsoptions).

## Host aliases of node

`render`, `koyeb`, `heroku`, `cleavr`, `flightcontrol`, `upsun`, `zerops`, `alwaysdata` and `digitalocean` are host aliases of `node`, like `cloud-run`: the build is the `node` build, byte for byte, and the log says `Building for node, host <name>`. `hamur.json` records `{ "preset": "node", "alias": "<name>" }`. Each is chosen by one environment variable, after every platform preset (so `RENDER=true` on a Vercel build still picks `vercel`):

| Alias           | Variable                    |
| --------------- | --------------------------- |
| `render`        | `RENDER`                    |
| `koyeb`         | `KOYEB_APP_NAME`            |
| `heroku`        | `DYNO`                      |
| `cleavr`        | `CLEAVR`                    |
| `flightcontrol` | `FLIGHTCONTROL`             |
| `upsun`         | `PLATFORM_APPLICATION_NAME` |
| `zerops`        | `ZEROPS_PROJECT_ID`         |
| `alwaysdata`    | `ALWAYSDATA_HTTPD_PORT`     |
| `digitalocean`  | `APP_PLATFORM`              |

`--preset <name>` names an alias when the variable is not set in the build. For `heroku` the log adds the line to put in the `Procfile`: `web: node .output/server/index.mjs`.
