77 # koa 中间件的应用

调用 next() 表示执行下一个中间件

javascript 复制代码
const Koa = require("koa");

const app = new Koa();

app.use(async (ctx, next) => {
    console.log(1);
    next();
    console.log(2);
});

app.use(async (ctx, next) => {
    console.log(3);
    next();
    console.log(4);
});

app.use(async (ctx, next) => {
    console.log(5);
    next();
    console.log(6);
});

app.listen(3000);

洋葱模型:

输出:135642

添加异步等待

javascript 复制代码
const Koa = require("koa");

const app = new Koa();

const log = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log("kaimo313");
            resolve();
        }, 3000);
    });
};

app.use(async (ctx, next) => {
    console.log(1);
    next();
    console.log(2);
});

app.use(async (ctx, next) => {
    console.log(3);
    await log();
    next();
    console.log(4);
});

app.use(async (ctx, next) => {
    console.log(5);
    next();
    console.log(6);
});

app.listen(3000);

输出:132 kaimo313 564

koa 中要求每个 next 方法前面都必须增加 await 否则不存在等待效果

javascript 复制代码
const Koa = require("koa");

const app = new Koa();

const log = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log("kaimo313");
            resolve();
        }, 3000);
    });
};

app.use(async (ctx, next) => {
    console.log(1);
    next();
    console.log(2);
    ctx.body = "hello 1";
});

app.use(async (ctx, next) => {
    console.log(3);
    await log();
    ctx.body = "hello 2";
    next();
    console.log(4);
});

app.use(async (ctx, next) => {
    console.log(5);
    ctx.body = "hello 3";
    next();
    console.log(6);
});

app.listen(3000);

会取中间件第一个执行完的结果

koa 的中间件原理:会将所有的中间件组合成一个大的 promise,当这个 promise 执行完毕后,会采用当前的 ctx.body 进行结果的响应(next 前面必须要有 await 或者 return 否则执行顺序可能达不到预期)

如果都是同步执行,加不加 await 都无所谓,由于不知道后续是否有异步逻辑,写的时候都要加上 await

next():

  1. 可以把多个模块通过 next 方法来链接起来
  2. 可以决定是否向下执行(可以用来实现后台的权限)
  3. 可以封装一些方法,在中间件中,封装向下执行

实现中间件计时器

javascript 复制代码
const Koa = require("koa");

const app = new Koa();

const log = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log("kaimo313");
            resolve();
        }, 3000);
    });
};

app.use(async (ctx, next) => {
    console.time("kaimo");
    await next();
    ctx.body = "hello 1";
    console.timeEnd("kaimo");
});

app.use(async (ctx, next) => {
    await log();
    ctx.body = "hello 2";
    return next();
});

app.use(async (ctx, next) => {
    ctx.body = "hello 3";
    return next();
});

app.listen(3000);
相关推荐
fishmemory7sec13 小时前
Koa2项目实战2(路由管理、项目结构优化)
数据库·mongodb·koa
栀夏61314 小时前
Kafka 快速入门
中间件
Dylanioucn16 小时前
【分布式微服务云原生】探索Redis:数据结构的艺术与科学
数据结构·redis·分布式·缓存·中间件
一水鉴天1 天前
智能工厂的软件设计 作为“程序Program”的中台
运维·人工智能·机器学习·中间件
加油,旭杏2 天前
【中间件学习】Nginx快速入门(为了配置一个项目)
学习·nginx·中间件
前端 贾公子2 天前
Express内置的中间件(express.json和express.urlencoded)格式的请求体数据
中间件·json·express
fishmemory7sec2 天前
Koa2+mongodb项目实战1(项目搭建)
数据库·mongodb·koa
-XWB-2 天前
【安全漏洞-中间件】nginx版本号屏蔽
运维·nginx·中间件
小春学渗透3 天前
安全服务面试总结
安全·网络安全·中间件·安全服务
是曹大大3 天前
【RocketMQ】RocketMQ的特性(顺序、事务等)
java·中间件·rocketmq