Quickstart
A first app with two routes, started on your runtime.
Install
bash
npm install routup
npm pkg set type=moduleThe second line marks the project as ESM, so Node.js loads server.ts without a module type warning. Other package managers and the requirements are on Installation.
Write the App
app.ts defines the routes and starts nothing, so a test or another runtime can use the same file:
ts
import { App, defineCoreHandler } from 'routup';
export const app = new App();
app.get('/', defineCoreHandler(() => 'Hello, World!'));
app.get('/users/:id', defineCoreHandler((event) => ({ id: event.params.id })));server.ts starts it:
typescript
import { serve } from 'routup';
import { app } from './app.ts';
serve(app);Run It
bash
node server.tsbash
bun server.tsbash
deno run --allow-net --allow-env server.tsbash
npx wrangler dev- Node.js runs
.tsfiles directly from 22.18 (and 23.6 on the 23 line). On an older 22 release, runnpx tsx server.ts. Node needs the.tsextension in relative imports. - Deno: the permission flags are explained on Runtime Environments.
- Cloudflare Workers use a different
server.ts: a module worker exports the server instead of starting it. The Cloudflare Workers example has the worker file and thewrangler.jsonc;wrangler devlistens on port 8787.
The server listens on port 3000 by default (serve options).
bash
curl http://localhost:3000/
# Hello, World!
curl http://localhost:3000/users/42
# {"id":"42"}What a Handler Returns
The string became a text/plain response and the object a JSON response. Every other value a handler can return (a Response, a stream, binary data, null) is listed on Response.
Next Steps
- App: HTTP methods, mounting and nesting
- Handlers: middleware with
event.next()and error handlers - Paths: parameters and wildcards
- Error Handling:
AppError, error handlers and the fallback response - Lifecycle Callbacks:
onBefore,onAfterandonErroraround one handler - Timeouts and Cancellation: request and handler timeouts,
event.signal - Testing: calling
app.fetch()from a test - Runtime Environments: the entry each runtime loads
- Examples: complete apps with tests