File tree Expand file tree Collapse file tree 6 files changed +86
-15
lines changed
Expand file tree Collapse file tree 6 files changed +86
-15
lines changed Original file line number Diff line number Diff line change 1+ const Koa = require ( 'koa' )
2+ const app = new Koa ( )
3+
4+ /**
5+ * logger
6+ * 第一个中间件,记录日志
7+ */
8+ app . use ( async ( ctx , next ) => {
9+ const middleRes1 = await next ( )
10+ console . log ( 'middleware1===>' , middleRes1 )
11+ const rt = ctx . response . get ( 'X-Response-Time' )
12+ console . log ( `${ ctx . method } ${ ctx . url } - ${ rt } ` )
13+ return 'back middleware1 result'
14+ } )
15+
16+ /**
17+ * x-response-time
18+ * 第二个中间件,记录响应时间
19+ */
20+
21+ app . use ( async ( ctx , next ) => {
22+ const start = Date . now ( )
23+ const middleRes2 = await next ( )
24+ console . log ( 'middleware2===>' , middleRes2 )
25+ const ms = Date . now ( ) - start
26+ ctx . set ( 'X-Response-Time' , `${ ms } ms` )
27+ return 'back middleware2 result'
28+ } )
29+
30+ /**
31+ * response
32+ * 最后一个中间件,处理响应
33+ */
34+ app . use ( async ctx => {
35+ ctx . body = 'Hello World'
36+ console . log ( 'middleware3===>' )
37+
38+ return 'back middleware3 result'
39+ } )
40+ app . listen ( 3000 )
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ export const KoaSidebar = [
2626 ]
2727 } ,
2828 {
29- text : '最佳实践'
29+ text : '最佳实践' ,
30+ link : 'https://code.142vip.cn'
3031 }
3132]
Original file line number Diff line number Diff line change @@ -5,16 +5,51 @@ permalink: /manuscripts/server-end/framework/koa-tutorial/application.html
55
66# Application应用对象
77
8+ ` Koa ` 应用程序是一个包含一组中间件函数的对象,它是按照类似` 堆栈 ` 的方式组织和执行的。
9+
10+ 如何理解这里的类似堆栈的方式呢?直接下面的例子:
11+
12+ @[ code js] ( @code/koa/koa-run-sort.js )
13+
14+ 当服务运行起来时,会先准备一个栈,等待着被调用,暂且称为` 调用栈 ` 。 在调用栈里面,三个中间件一次进入栈中,
15+ 其中第一个中间件会被压到栈底。
16+
17+ 进入栈的顺序(中间件1在栈底):
18+
19+ ``` mermaid
20+ ---
21+ title: 中间件调用流程
22+ ---
23+ stateDiagram-v2
24+ 客户端 --> 中间件1
25+ 中间件1 --> 中间件2
26+ 中间件2 --> 中间件3
27+ 中间件3 --> 服务端
28+ ```
29+
30+ 执行调用的顺序(栈顶的中间件函数先执行):
31+
832``` mermaid
933---
1034title: 执行流程
1135---
12- flowchart TB
13- a1-->a2
14- a2-->a1
15-
36+ flowchart TD
37+ 服务端-->中间件3 --> 中间件2--> 中间件1 --> 客户端
1638```
1739
40+ 其中,在中间的处理函数中存在两个参数:
41+
42+ - ` ctx ` 当前请求的上下文对象
43+ - ` next ` 下一个中间件的执行器
44+
45+ 利用` next() ` 可以获取到下一个执行函数的返回结果。例如:在中间件2中可以拿到中间件3的返回结果
46+
47+ ![ ] ( ../images/koa-run-sort.png )
48+
49+ 到这里应该就能慢慢地理解koa的app对象是如何工作的、调用链路、顺序是怎样的,这也就是来的洋葱圈模型
50+
51+ 此刻,想到那首歌:”如果你愿意一层一层地剥开我的心,你会发现、你会讶异....“
52+
1853## 设置App属性
1954
2055在使用koa时,一般是利用Koa模块new一个app对象
Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ export const ServerEndSidebar = [
1010 } ,
1111 {
1212 text : 'ES6' ,
13- // collapsible: true,
1413 link : 'es6'
1514 } ,
1615 {
Original file line number Diff line number Diff line change @@ -8,12 +8,12 @@ actions:
88 - text : 快速开刷 →
99 link : /quick-start.md
1010 type : primary
11- - text : 工作机会
11+ - text : 工作机会💡
1212 link : /manuscripts/job-chance/job-poster-bytedance.md
1313 type : secondary
14- - text : 公众号文章💡
15- link : /manuscripts/wechat-list.md
16- type : secondary
14+ # - text: 公众号文章💡
15+ # link: /manuscripts/wechat-list.md
16+ # type: secondary
1717
1818features :
1919 - title : 前端
@@ -34,12 +34,8 @@ features:
3434 details : 尝试尝试再尝试,等等我呀...
3535---
3636
37-
38-
39-
4037[ // ] : # ( <BiliBili bvid="BV1rd4y1V7tB" /> )
41-
42- [ // ] : # ( <XiGua id="7191010629048716605" /> )
38+ [ // ] : # ( <XiGua id="7195198927220834868" /> )
4339
4440## 本地浏览【推荐】
4541
You can’t perform that action at this time.
0 commit comments