Koa

Question:

1. what's the different between koa and express?
2. how does koa handle with callbacks?
3. what does downstram and upstream mean?
4. "with generators we can achieve "true" middleware. " How?
...

Notes

Introduction

Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling.

A Koa application is an object containing an array of middleware generator functions which are composed and executed in a stack-like manner upon request. 

Cascading

Koa middleware cascade in a more traditional way as you may be used to with similar tools - this was previously difficult to make user friendly with node's use of callbacks. 
However with generators we can achieve "true" middleware. 
Contrasting Connect's implementation which simply passes control through series of functions until one returns, Koa yields "downstream", then control flows back "upstream".

yields 按顺序“向下”执行,而后控制流“向上”冒泡。即上游的中间件必须等到下游的中间件返回结果或全部执行完成才会继续执行,这点很像递归。

如果想跳过一个中间件,直接写上return yield next; 则中间件内yield next后的语句都不会再执行。

如果一个中间件缺少yield next语句,后面的中间件都不会执行。

路由

简单的路由用this.path配合中间件即可,复杂的路由可使用koa-router

对象 context, request, response

读源码

Figure out these:

1. 设计之原理
2. 机巧之细节

既然Koa是Node.js的一个框架,是巨人肩膀上长成的。首先要了解Node.js,才知道Koa所做扩展和封装的意义。

目的

一个框架应该提供什么样的功能?

results matching ""

    No results matching ""