typescript - 在一个变量中调用多个中间件/参数 - deno Oak 框架
问题描述
我有这个代码
const keren = async (ctx: Context, next: any) => {
console.log('keren');
await next();
}
const mantap = async (ctx: Context, next: any) => {
console.log('mantap');
await next();
}
router.get('/owkowkkow',keren,mantap,(ctx: Context) => {
ctx.response.body = "wkwkwkw";
});
它工作得很好,但我想在一个名为 onevar 的变量中使用 keren 和 mantap
所以代码会是这样的:
router.get('/owkowkkow',onevar,(ctx: Context) => {
ctx.response.body = "wkwkwkw";
});
怎么做?可以吗?
解决方案
Oak 带有compose
中间件,允许您从多个中间件组成一个中间件
import { composeMiddleware as compose } from "https://deno.land/x/oak/mod.ts";
const onevar = compose([
async (ctx: Context, next: any) => {
console.log('keren');
await next();
},
async (ctx: Context, next: any) => {
console.log('mantap');
await next();
}
])
router.get('/owkowkkow',onevar,(ctx: Context) => {
ctx.response.body = "wkwkwkw";
});
推荐阅读
- android-studio - SQL android studio中的多个表
- android - 为什么 API 28 平台资源不可用?
- amazon-web-services - 仅允许启动/启动/停止/终止特定实例类型的 EC2 实例
- ssis - SSIS 可以导出到 PowerPoint 幻灯片吗
- elasticsearch - 如何丢弃带有空字段的结果?
- ruby-on-rails - Watir 与 Rails
- c# - System.Text.Json 中的 Newtonsoft.Json DefaultValueHandling = DefaultValueHandling.Ignore 选项的等价物是什么
- reactjs - Formik - 在 Formik 之外渲染复选框组
- c# - CEFSharp 打开新窗口
- ruby - 将域名更改为 Ruby 应用程序的问题