hono Req
我们在使用 hono req 的时候有两种用法:
c.req
:是 hono 封装 HonoRequest 对象能够被消费多次c.req.raw
是 Request 对象,特点是像 json 只能消费一次
使用 c.req
消费数据
ts
docsRouter.post(
"/changelog",
zValidator("json", changelogSchema.create),
permission(docsPermissions.changelog.CREATE),
async (c: Context) => {
try {
const dto = await c.req.json();
const userId = c.get("userId");
const data = {
...dto,
userId,
};
const result = await changelogDAL.create(data);
return c.json({
data: result,
message: "success",
code: 0,
});
} catch (error) {
return c.json({
data: null,
message: (error as Error).message ?? "获取失败",
code: 1,
});
}
},
);
这里已 json 为示例,注意,如果你私用 c.req.raw.json
消费的话,会造成 只能消费一次的而报错的情况
。
这里是 hono context
api 的文档。