Modular Apps
Routes split into one App per resource, mounted on a root App under a path prefix.
ts
import { App, defineCoreHandler } from 'routup';
export const users = new App();
users.get('/:id', defineCoreHandler((event) => ({
id: event.params.id,
name: 'Alice',
})));
export const posts = new App();
posts.get('/:id', defineCoreHandler((event) => ({
id: event.params.id,
title: 'Hello',
})));
export const app = new App();
app.use('/users', users);
app.use('/posts', posts);Try It
Install routup, 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 http://localhost:3000/users/1
# {"id":"1","name":"Alice"}
curl http://localhost:3000/posts/1
# {"id":"1","title":"Hello"}How It Works
app.use('/users', users)registers each route ofusersonappwith/usersin front of its path.- Mounting copies the routes at the call. A route added to
usersafterapp.use()answers onusersbut is 404 onapp, so register a child's routes before mounting it. See Nesting.