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 onlyexport default
will be updated intensively.
Typescript
export default [(ctx) => {}];
#context.
Property/Method | Description | Same |
---|---|---|
context.status | Set HTTP status code | res.status |
context.headers | Set HTTP headers | res.headers |
context.body | Access response body | res.body |
context.params | Route parameters | res.params |
context.query | Query string parameters | res.query |
context.props | Static props for rendering | res.props |
Methods | ||
context.send() | Send a response | res.end() |
context.header() | Set a specific header | res.setHeader() |
context.html() | Send an HTML response | Custom logic |
context.json() | Send a JSON response | res.json() |
context.text() | Send a plain text response | res.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