当前位置 : 主页 > 编程语言 > java >

Koa(三):执行顺序

来源:互联网 收集:自由互联 发布时间:2022-07-17
示例 const Koa = require('koa'); const router = require('koa-router')(); const app = new Koa(); app.use(async (ctx, next) = { console.log('01'); next(); console.log('001'); }) app.use(async (ctx, next) = { console.log('02'); await next();


示例

const Koa = require('koa');

const router = require('koa-router')();

const app = new Koa();

app.use(async (ctx, next) => {

console.log('01');

next();

console.log('001');

})

app.use(async (ctx, next) => {

console.log('02');

await next();

console.log('002');

})

// 配置路由
router
.get('/', async (ctx, next) => {

console.log('03');

next();

console.log('003');

})
.get('/', async (ctx, next) => {

console.log('04');

ctx.body = 'Hello';

})

app
.use(router.routes())
.use(router.allowedMethods())
.listen(3000);

console.log('Server run in http://localhost:3000/');

执行顺序

  • ​​http://localhost:3000/​​ 控制台输出
  • ​​01 - 02 - 03 - 04 - 003 - 002 - 001​​
  • 形象图:
  • Koa(三):执行顺序_nodejs


【本文由:高防cdn http://www.558idc.com/gfcdn.html 复制请保留原URL】
上一篇:Dubbo基础入门介绍
下一篇:没有了
网友评论