PeaBox Lab › PeaLang › Learn › Chapter 28 › Lesson 1

Part V — The Modern Experience Layer · Chapter 28 · Lesson 1 of 3

Routing, request helpers and responses

std.web is the HTTP layer that ships inside PeaLang. 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

import web

let app = web.app()

app.get("/health", fn(req) {
  return web.json({ "ok": true })
})

web.serve(app, 8080)

web.app() builds the application object; app.get/post/put/delete/patch/options register routes; web.serve runs it. Route handlers receive a request and return a response — that is the whole contract.

Path parameters

app.get("/users/:id", fn(req) {
  let id = req.params.id
  let user = find_user(id)
  if user == null {
    return web.not_found()
  }
  return web.json(user)
})

OpenAPI-style {id} placeholders are converted automatically — you can write either form and web.openapi(app, ...) emits the spec in OpenAPI 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 prefixes and, if you attach middleware to the group, authorization and logging apply to all of them at once.

Request helpers

req.query, req.params, req.headers, req.cookies and body readers cover the common cases — JSON body, form body, raw text. URL construction helpers (web.url_join, web.build_query, web.parse_query) sit next to them. web.query_int and web.query_bool parse typed query params with defaults.

Response helpers

Static files, middleware, 404s

app.static("/assets", "./public") serves a directory. Middleware runs in registration order — apply web.request_logger first, then auth, then your handler, then the error handler last. Register app.not_found(fn(req) { ... }) for the catch-all 404 and app.error(fn(err, req) { ... }) for 500s; both should log and return a problem document, never a stack trace.

CORS

app.enable_cors({
  "origins": ["https://app.example.com"],
  "methods": ["GET", "POST"],
  "allow_credentials": true
})

Preflight handling is included. Do not reach for origins: ["*"] with credentials — that combination is invalid and browsers reject it; echo an explicit allow-list instead.

Guided practice

← Previous Next →