Docs
Routing
Sending Data
Sending Data
#Data types
-
Available types:
string
,number
,object(JSON)
,boolean
,bigint
-
Unavailable types:
undefined
,function
,class
, etc...
#res.send()
This is the most basic sending method.
It can be used as ctx.send
or ctx.response.send
.
Typescript
export default [
GET((ctx) => {
ctx.send({});
}),
];
ERROR
ctx.response.end
and ctx.response.send
are completely different.
ctx.response.end
is a function sending data provided by default in nodejs.
#return
Another way to send data is return. Just return the data you want to send!
The return
values will be sent through res.send
.
Typescript
export default [
GET(() => {
return { msg: '👋' };
}),
];
#response()
You can set more values ​​(like headers) in the response by creating a response instance.
Typescript
import { response } from 'zely';
export default [
GET(() => {
return response(
{
/* data */
},
{
/* headers */
},
);
}),
];
TIP
Example:
Typescript
import { response } from 'zely';
export default [
GET(() => {
const res = response(
{ msg: '👋' },
{
/*headers*/
},
);
res.body = { msg: '🎉' };
res.headers = {};
res.status = 500;
return res;
}),
];