Koa01 中间件

本文介绍了Koa中间件的概念,包括其执行顺序、异步处理、错误处理和中间件开发。中间件形成栈结构,按先进后出顺序执行。在异步中间件中,需使用Async函数。错误处理通常由最外层中间件负责。文章还探讨了中间件引擎的简单实现,核心是洋葱模型,并提供了源码解读。
摘要由CSDN通过智能技术生成

概念

Koa中间件的最大特色就是中间件(middleware)的设计。

中间件是一个函数,它处在HTTP Request和HTTP Response中间,用来实现某种中间功能,通过app.use()来加载中间件。

const Koa = require('koa');

const app = new Koa();

app.use(async (ctx) => {
  ctx.response.body = 'GO'
});

app.listen(8080, () => {
  console.log('app is listening 8080...');
});

中间件的执行顺序

多个中间件会形成栈结构,以先进后出的顺序执行:

  1. 最外层的中间件首先执行
  2. 代用next函数,把执行权交给下一个中间件
  3. 最内层的中间件最后执行
  4. 执行结束后,把执行权交回上一层的中间件
  5. 最外层的中间件收回执行权后,执行next函数后面的代码

看下面的例子:

app.use(async (ctx, next) => {
  console.log(1-1);
  ctx.response.body = 'GO';
  next();
  console.log(1-2);
});

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

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

app.listen(8080, () => {
  console.log('app is listening 8080...');
});

执行结果是:

1-1
2-1
3-1
3-2
2-2
1-2

这种先进后出的加载模型也可以叫做洋葱圈的模型:

如果中间件内部没有调用next函数,那么执行权就不会传递下去。

异步中间件

当中间件中包含异步操作时,中间件应该写成Async函数:

app.use(async (ctx, next) => {
  ctx.response.body = await fse.readFile('../demo3/test.txt', 'utf-8');
});

注意,如果调用next,必须等待完成

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

app.use((ctx) => {
  ctx.response.body = await fse.readFile('../demo3/test.txt', 'utf-8');
});

如果是上面的形式,返回的body中将没有任何内容,这是因为Koa在Promise链被机械系了之后就结束了请求,这意味着我们在设置ctx.response.body之前,response就已经被发送了给客户端,正确的做法应该是在第一个中间件的next之前添加await

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

app.use((ctx) => {
  ctx.response.body = await fse.readFile('../demo3/test.txt', 'utf-8');
});

当使用纯粹的Promise(不使用Async/Await)应该写成这样:

app.use((ctx, next) => {
  ctx.statu
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值