Docs
Routing
Context

Context

Starting from v2, the parameter context is provided instead of req and res in handlers exported by default.

Note: The context function is only supported in export default.

Just export will no longer be updated and only export default will be updated intensively.

Typescript
export default [(ctx) => {}];

#context.

Property/MethodDescriptionSame
context.statusSet HTTP status coderes.status
context.headersSet HTTP headersres.headers
context.bodyAccess response bodyres.body
context.paramsRoute parametersres.params
context.queryQuery string parametersres.query
context.propsStatic props for renderingres.props
Methods
context.send()Send a responseres.end()
context.header()Set a specific headerres.setHeader()
context.html()Send an HTML responseCustom logic
context.json()Send a JSON responseres.json()
context.text()Send a plain text responseres.text()

#Request and response

Typescript
export default [
  (ctx) => {
    ctx.request; // request
    ctx.response; // response
  },
];

#Migration

Plain
export default [
-  (req, res) => {
+  (ctx) => {
-    res.end("Hello World!");
+    ctx.send("Hello World!");
  },
];

#Reference

Reference: /server/context.ts
Reference: /types/core.d.ts