how koajs works @cc http://code.tutsplus.com/tutorials/introduction-to-generators-koajs-part-1--cms-21615

u.go
2015-12-28T06:45:06Z

koajs-hello.js

| var koa = require('koa')();
|
| koa.use(function* (next) {
| //do something before yielding/passing to next generator function in line which will be 1st event in downstream
| console.log("===1===");
| console.log("A");
| yield next;
|
| // do something when the execution returns upstream, this will be last event in upstream
| console.log("B");
| console.log("===/1==");
| });
|
| koa.use(function* (next) {
| // do something before yielding/passing to the next generator function in line, this shall be 2nd event downstream
| console.log("====2==");
| console.log("C");
|
| yield next;
|
| // do something when the execution returns upstream and this would be 2nd event upstream
| console.log("D");
| console.log("====/2==");
| });
|
| koa.use(function* () { // do something before yielding/passing to next generator function in line. Here it would be last function downstream
| console.log("====3==");
| console.log("E");
| this.body = "hey guys";
| console.log("F"); // First event of upstream (from the last to first)
| console.log("====/3==");
| });
|
| koa.listen(3000);
|
| // ===1===
| // A
| // ====2==
| // C
| // ====3==
| // E
| // F
| // ====/3==
| // D
| // ====/2==
| // B
| // ===/1==