REST API
CRUD routes for todos over an in-memory store. Request bodies are read with @routup/body, and an unknown id answers 404.
ts
import { App, AppError, defineCoreHandler } from 'routup';
import type { IAppEvent } from 'routup';
import { body, readRequestBody } from '@routup/body';
type Todo = {
id: number,
title: string,
done: boolean,
};
const todos = new Map<number, Todo>();
let nextId = 1;
function findTodo(event: IAppEvent): Todo {
const id = Number(event.params.id);
const todo = todos.get(id);
if (!todo) {
throw new AppError({ status: 404, message: `Todo ${event.params.id} not found` });
}
return todo;
}
function readTitle(value: unknown): string {
if (typeof value !== 'string' || value.length === 0) {
throw new AppError({ status: 400, message: 'title must be a non-empty string' });
}
return value;
}
function readDone(value: unknown): boolean {
if (typeof value !== 'boolean') {
throw new AppError({ status: 400, message: 'done must be a boolean' });
}
return value;
}
export const app = new App();
app.use(body());
app.get('/todos', defineCoreHandler(() => todos.values().toArray()));
app.post('/todos', defineCoreHandler(async (event) => {
const body = await readRequestBody(event);
const title = readTitle(body.title);
const todo: Todo = {
id: nextId++,
title,
done: false,
};
todos.set(todo.id, todo);
event.response.status = 201;
return todo;
}));
app.get('/todos/:id', defineCoreHandler((event) => findTodo(event)));
app.patch('/todos/:id', defineCoreHandler(async (event) => {
const todo = findTodo(event);
const { title, done } = await readRequestBody(event);
const nextTitle = title === undefined ? todo.title : readTitle(title);
const nextDone = done === undefined ? todo.done : readDone(done);
todo.title = nextTitle;
todo.done = nextDone;
return todo;
}));
app.delete('/todos/:id', defineCoreHandler((event) => {
todos.delete(findTodo(event).id);
event.response.status = 204;
return null;
}));Try It
Install routup and @routup/body (the plugin needs Node.js 24 or later), serve the app from a second file and start it with your runtime as on the Quickstart:
typescript
import { serve } from 'routup';
import { app } from './app.ts';
serve(app, { port: 3000 });bash
curl -X POST http://localhost:3000/todos \
-H 'content-type: application/json' \
-d '{"title":"Write docs"}'
# {"id":1,"title":"Write docs","done":false}
curl -X PATCH http://localhost:3000/todos/1 \
-H 'content-type: application/json' \
-d '{"done":true}'
# {"id":1,"title":"Write docs","done":true}
curl http://localhost:3000/todos/99
# {"status":404,"message":"Todo 99 not found"}How It Works
body()turns on the JSON and URL-encoded parsers;readRequestBody(event)returns{}when the request'scontent-typematches neither, so thecurlcalls set it. See Reading the Body.event.response.statussets the status of the response built from the return value. See Status and Headers.- Returning
nullsends an empty body with the status set onevent.response. See Return Values. - The
AppErroroffindTodo()(404) and ofreadTitle()andreadDone()(400) reaches no error handler, so it becomes the fallback response with its status.