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);
相关推荐
fuquxiaoguang17 小时前
中间件的“结构性升级”:当智能编排成为政企采购的“硬指标”
中间件·ai智能体·ai原生
C137的本贾尼20 小时前
第七篇:消息队列(MQ)——就是个带存储的异步通信管道
java·开发语言·中间件
半夜修仙1 天前
RabbitMQ的推模式和拉模式
java·分布式·中间件·rabbitmq·github·java-rabbitmq
Sagittarius_A*2 天前
Web 渗透实战:服务器系统识别、端口与中间件全量探测
服务器·网络安全·中间件·渗透测试
马里马里奥-3 天前
大模型应用开发笔记04:LangChain 工具(Tool)与中间件(Middleware)详解
笔记·中间件·langchain
霸道流氓气质3 天前
CDC 中间件捕获 Binlog 全流程详解
中间件
名字还没想好☜5 天前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
马里马里奥-5 天前
大模型应用开发笔记03:LangChain 中间件实战
笔记·中间件·langchain
heimeiyingwang6 天前
【架构实战】分库分表实战:ShardingSphere与中间件选型
中间件·架构
斯蒂文6686 天前
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
缓存·中间件