Request context
The ctx object received by middlewares and handlers, and its req and res. For an introduction, see The request context.
RequestContext
In a handler, ctx also holds the properties guaranteed by the typed middlewares of the route and its groups.
Keys added by plugins
Plugin keys are computed on first access, at most once per request. Using the key of a plugin that is not installed throws:
req
Middlewares run before validation: ctx.req.params, query and body hold the raw values, typed as such. Handlers receive the parsed values.
Request bodies are parsed by content type: JSON (application/json, *+json), forms (application/x-www-form-urlencoded, multipart/form-data), text (text/*, as a string), anything else as bytes. See Bodies and file uploads.
res
ctx.res never writes the body: the handler returns it. Every method returns res, so calls can be chained.
Headers and cookies set on res are kept on the response, including an HttpError returned or thrown later; an unexpected error gives a 500 without them.
Status
The status of a JSON response is, in this order: the status of json(data, { status }), then res.setStatus(), then the status of the route, then 200. A handler returning undefined gets 204, unless a status is set.
Cookie options
See Cookies, and Cookie mode for authentication.
Extending the context
Declare a key with module augmentation, then provide it from a plugin with api.decorateContext(), or set it in a middleware:
declareContextKey(key, pluginName) makes the key throw an explicit error when the plugin is not installed. The keys of the core (req, res, requestId, logger, raw, publicPath) cannot be decorated. See Adding your own keys.