# Node middleware

`--preset node-middleware` mounts the app inside a Node server you already run. It is chosen by name only.

## Reference

|                      |                                                       |
| -------------------- | ----------------------------------------------------- |
| Preset               | `node-middleware`                                     |
| Runs on              | a Node server that hosts Hamur next to its own routes |
| `event.runtime.name` | `node-middleware`                                     |
| Server target        | `node`                                                |
| Lifetime, startup    | `process`, `request`                                  |

### Output

```text
.output/
  hamur.json
  server/index.mjs           export { handler, fetch, close, middleware }
  public/
```

### Config keys

This preset has no key of its own.

### 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. Import `middleware` or `handler` from `.output/server/index.mjs` into your Node server. `hamur preview` runs the built output with Hamur's own runner.

### Limits

- Runs: streamed answers.
- Experimental: private `app/env.ts` entries read at run time.
- Does not run: WebSockets and `server.syncedState`, `server.pubsub`, `server.metrics`, image transforms at run time, Durable Object classes.

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)).

```
.output/server/index.mjs   export { handler, fetch, close, middleware }
```

The entry never listens on import. `handler(req, res, next?)` is a `node:http` request listener, `fetch(request)` takes a Web `Request`, `close()` runs the `close` hook and flushes the logs, and `middleware({ fallthrough: true })` returns a Connect-style `(req, res, next)` that calls `next()` where no server route or page matched, instead of answering the 404 page. Hamur does not serve `.output/public/`; serve it from the host. A body of a request that falls through may already have been read, so mount Hamur last for `POST` routes the host handles. `tasks.runOnStart` fails the build: `tasks.runOnStart is not supported on the "node-middleware" preset: the host starts the process; call the task from the host`.

```
import { createServer } from "node:http";
import { handler } from "./.output/server/index.mjs";

createServer(handler).listen(3000);
```

Express and any Connect-style host take the middleware:

```
import express from "express";
import { middleware } from "./.output/server/index.mjs";

const app = express();
app.get("/health", (_req, res) => res.send("ok"));
app.use(middleware({ fallthrough: true }));
app.listen(3000);
```

A host with a Web `fetch` handler calls `fetch(request)`. Hono: `app.mount("/", fetch)`. h3: `app.use((event) => fetch(event.req))`. Fastify: reply with `handler(request.raw, reply.raw)` after `reply.hijack()`. These recipes need the host's own package in your app; Hamur depends on none of them.
