docs
routing
Methods
Methods
Creating a page that prints hello world
requires us to write more code than expected.
Typescript
export function get(req, res) {
res.end('Hello, World!');
}
In these situations, methods
can be used to write more concise code.
Typescript
export default 'Hello, World!';
Not only that, methods
can be used flexibly in many situations.
#all
, get
, post
...
Just exporting default with nothing responds to all methods. However, if you want to respond only when method is GET
, you can write the following code.
#Multiple Handlers
If you want some functions to only respond to GET
and other functions to only respond to POST
, you can put them in an array.
Typescript
import { GET, POST } from 'zely';
export default [
GET({
type: 'cat',
emoji: '🐱',
}),
POST({
type: 'dog',
emoji: '🐶',
}),
];
#Global import
If importing handlers per page feels complicated, enable the options.globalImport
option.
zely.config.ts
export default {
globalImport: true,
};
Then, you can use GET
, POST
without importing handlers.
Typescript
export default [GET(/* ... */)];
TIP
If you are using typescript, create env.d.ts
and add the following code.
Typescript
import "zely/env";
JSON
// tsconfig.json
{
"include": ["./env.d.ts"]
}
#Access to req
, res
Functions can be used as the body.
Typescript
import { POST } from 'zely';
export default [POST((ctx) => {
ctx.send(req.body)
}];