App
Simple and intuitive routing for handling HTTP requests.
var app = new App(globalOptions?);
app.get(path, handler, routeOptions?);
app.post(path, handler, routeOptions?);
// ...
export default app;
Global Options
new App(globalOptions?)
| Option | Description |
|---|---|
| maxBodySize | Max request body size (default: 1MB) |
| auth | Authentication function |
| headers | Response headers |
| gzip | Enable gzip compression |
Routing
app.method(path, handler, routeOptions?)
Route Options
Override or merge with global options.
| Option | Description |
|---|---|
| maxBodySize | Override global |
| rewrite | Path rewrite (route only) |
| auth | Override global |
| headers | Merge with global |
| gzip | Override global |
Route Methods
get, post, put, delete, patch, head, options, all
Path Patterns
| Pattern | Example | params |
|---|---|---|
| Static | /users |
{} |
| Named param | /users/:id |
{id: "123"} |
| Wildcard | /files/* |
{wildcard: "a/b/c"} |
handler(request, params)
// Sync
function handler(request, params) {
return new Response("id=" + params.id);
}
// Async
async function handler(request, params) {
const body = await request.text();
return new Response(body);
}
request object
| Property/Method | Description |
|---|---|
| method | HTTP method (GET/POST/...) |
| url | scheme://host/path?query |
| headers | Headers object |
| text() | Read body as text |
| json() | Read body as JSON |
| bodyUsed | Whether body has been read |
url format:
- scheme: http or https (based on connection)
- host: From Host header
- path?query: Original request path
params object
Route parameter key-value pairs.
Option Features
rewrite
Path rewrite, changes the path part of request.url.
app.get("/users/:id", handler, { rewrite: "/api/v2/users/${id}" });
- Use
${name}to reference route params - Query string handling:
?: auto-append original query
- rewrite with ?: no append
| Request | rewrite | request.url path |
|---|---|---|
/users/123 |
/api/${id} |
/api/123 |
/users/123?debug=1 |
/api/${id} |
/api/123?debug=1 |
/users/123?debug=1 |
/api/${id}?v=2 |
/api/123?v=2 |
auth
Authentication function. Runs before handler. Route auth overrides global.
false- No authfunction(request, params)- Auth function, return 4xx to reject, others allow
// Simple auth
app.get("/admin", handler, {
auth: (req, params) => req.headers.get("token") === "secret" ? null : 401
});
// Multiple status codes
app.get("/api", handler, {
auth: (req, params) => {
const token = req.headers.get("authorization");
if (!token) return 401; // Unauthorized
if (token !== "valid") return 403; // Forbidden
}
});
// Async
app.get("/resource/:id", handler, {
auth: async (req, params) => {
const user = await validateToken(req.headers.get("token"));
if (!user) return 401;
if (!user.canAccess(params.id)) return 403;
}
});
// Global auth with public route
var app = new App({
auth: (req, params) => req.headers.get("token") ? null : 401
});
app.get("/public", handler, { auth: false });
headers
Response headers. Route options merge with global, route takes precedence.
var app = new App({ headers: { "X-Global": "1" } });
app.get("/api", handler, { headers: { "X-Route": "2" } });
// Response has both X-Global: 1 and X-Route: 2
Supports template variable ${name} for route params.
gzip
Gzip compression. Requires client support (Accept-Encoding: gzip).
var app = new App({ gzip: true });
app.get("/raw", handler, { gzip: false }); // Disable for this route