Part V — The Modern Experience Layer · Chapter 28 · Lesson 1 of 3
Routing, request helpers and responses
The web namespace is the HTTP layer that ships inside PeaLang (also available as std.web). It is not Express and does not try to be: it covers app registration, routing, request/response helpers, middleware and error handling. If you want a framework, keep your host in TypeScript or Python; if you want a small, auditable HTTP surface for your own service, this is it.
An app in four lines
let app = web.app()
app.get("/health", function(req) {
return web.json({ "ok": true })
})
app.serve("127.0.0.1", 8080)
web.app() builds the application object; app.get/post/put/delete/patch/options register routes; app.serve(host, port) runs it. Route handlers receive a request dict and return a response; plain dicts and strings are normalized into responses too.
Path parameters
app.get("/users/:id", function(req) {
let id = req["params"]["id"]
let user = find_user(id)
if user == null {
return web.not_found()
}
return web.json(user)
})
The router accepts :id and {id} placeholders. In PeaLang source, prefer :id: a literal {id} inside a string is parsed as interpolation. web.openapi(app, title, version) emits the spec with OpenAPI-style {id} notation.
Route groups
let api = app.group("/api")
api.get("/orders", list_orders)
api.post("/orders", create_order)
api.get("/orders/:id", get_order)
Groups share a prefix for get/post/put/delete/patch. Middleware is registered on the app with app.use(...) and applies to every route.
Request helpers
Handlers receive a dict: req["query"], req["params"], req["headers"], req["cookies"], req["json"], req["body"], req["path"], req["method"] and req["remote_addr"]. Use web.parse_json(text) for raw JSON and web.parse_query(text) for query strings. URL helpers (web.url_join, web.build_query) sit next to them, and web.query_int(req, name, default) and web.query_bool(req, name, default) read typed query params.
Response helpers
- Content —
web.json,web.html,web.text,web.file,web.sse. - Status shortcuts —
web.created,web.accepted,web.no_content,web.redirect,web.download,web.status. - Errors —
web.bad_request,web.unauthorized,web.forbidden,web.not_found,web.conflict,web.too_many_requests,web.method_not_allowed,web.unprocessable,web.server_error, andweb.problem(title, status, detail, type_)for a problem+json document withtypeandtitlefields.
Static files, middleware, 404s
app.add_static("/assets", "./public") serves a directory. Register middleware with app.use(...); it runs in registration order — web.request_logger() is a ready-made logger. Register app.not_found(function(req) { ... }) for the catch-all 404 and app.on_error(function(req) { ... }) for failures; both handlers receive the request, so return web.problem(...) rather than a stack trace.
CORS
app.enable_cors("https://app.example.com", "GET,POST", "Content-Type,Authorization")
The three arguments are strings: origins, methods and allowed headers. Preflight handling is included — an unregistered OPTIONS request gets a 204 with the CORS headers. Use an explicit origin string: the helper sets Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers, and adds no credential handling.
Guided practice
- Stand up a two-route service (
/health,/echo) and confirm both return well-formed JSON. - Add a route group under
/api/v1and registerweb.request_logger()withapp.use(...)to log method and path. - Register a custom 404 with
app.not_found(...)and verify it returns aweb.problem(...)document withtypeandtitlefields.