docs: serving restloom from plain Node, alone or beside other routes

This commit is contained in:
2026-11-03 12:27:02 +01:00
parent 667df8325a
commit 2530941f22
2 changed files with 16 additions and 4 deletions
+15 -3
View File
@@ -9,14 +9,26 @@ const response = await rest.handle({ method, path, query, headers, body, context
`handle` never throws: every failure is a response. `path` is the path below where the API is mounted (`/todos/4`), `query` a `URLSearchParams`, `body` the parsed JSON.
## Node
## Node, without a framework
`nodeListener` from the core serves `node:http` with no other dependency. It reads JSON bodies up to 1 MiB (`maxBodyBytes`).
`nodeListener` from the core serves `node:http` with nothing else installed. It mounts the API under `prefix` and answers `404 not_found` outside it, reads JSON bodies up to 1 MiB (`maxBodyBytes`), and builds each request's context with `context`:
```ts
http.createServer(nodeListener(rest, { prefix: '/api', context: (req) => userFrom(req) })).listen(3000);
import http from 'node:http';
import { createREST, nodeListener } from '@crudy/restloom';
const api = nodeListener(rest, { prefix: '/api', context: (req) => userFrom(req) });
http.createServer(api).listen(3000);
```
To serve other pages from the same server, hand only the API's paths to it:
```ts
http.createServer((req, res) => (req.url?.startsWith('/api/') ? api(req, res) : site(req, res))).listen(3000);
```
Without HTTP at all, call `rest.handle` directly: [From your own code](#from-your-own-code).
## Fastify
```sh
+1 -1
View File
@@ -31,7 +31,7 @@ const rest = createREST({
http.createServer(nodeListener(rest, { prefix: '/api' })).listen(3000);
```
Every table and view of `app` is now a resource, read-only:
`nodeListener` comes with the core, so no framework is needed. Every table and view of `app` is now a resource, read-only:
```sh
curl -g 'http://localhost:3000/api/todos?select=id,name,owner(name)&status=eq.open'