Logger and CORS
Two built-in middleware: request logging and cross-origin headers.
loggerMiddleware
Logs one line per request with the timestamp, method, path, status and duration.
TypeScript
import { loggerMiddleware } from 'riser-bob' bob.spongUse(loggerMiddleware)Output
[RiserBob] 2026-01-01T12:00:00.000Z | GET /users | 200 | 1.42msIf something throws further down the chain, the logger prints the error and rethrows it so the central error handler can answer. A request that fails this way is logged with status 500.
corsMiddleware
Adds the CORS headers a browser needs to call your API from another origin.
TypeScript
import { corsMiddleware } from 'riser-bob' bob.spongUse( corsMiddleware({ origin: ['http://localhost:5173'], credentials: true, }))| Option | Type | Default |
|---|---|---|
origin | string | string[] | '*' | Required |
credentials | boolean | false |
methods | string[] | GET, POST, PUT, PATCH, DELETE |
allowedHeaders | string[] | Content-Type, Authorization |
exposedHeaders | string[] | [] |
maxAge | number (seconds) | 86400 |
origin can be one string, such as 'http://localhost:5173', a list of origins, or '*'.
- Headers are only added when the request origin is allowed.
- When
originis not'*', the response includesVary: Origin. - Invalid options throw a
TypeErrorwhen the middleware is created, not on the first request. This covers'*'with credentials, an emptymethodslist, and amaxAgethat is not a non-negative integer. - Preflight
OPTIONSrequests are answered by the server before routing. See Routing.
Order matters
Register both with spongUse before any route. A common setup is CORS first, then the logger:
src/index.ts
const bob = new RiserBob(); bob.spongUse( corsMiddleware({ origin: "http://localhost:5173", credentials: true, }),);bob.spongUse(loggerMiddleware); bob.spongGet("/", (spongReq, spongRes) => spongRes.send("Welcome"));When a browser app on another origin sends cookies, the front end must also opt in, for example fetch(url, { credentials: 'include' }).