-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_koa.js
47 lines (40 loc) · 1.07 KB
/
7_koa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var http = require('http');
var koa = require('koa');
var app = koa();
app.use(function * (next) {
// Koa runs middleware in an co
// context. This means we can
// do here anything that we learned in
// the co samples.
console.log( 'middleware 1 DOWN' );
yield next;
console.log( 'middleware 1 UP' );
});
app.use(function * (next) {
console.log( 'middleware 2 DOWN' );
yield next;
console.log( 'middleware 2 UP' );
});
app.use(function * (next) {
console.log( 'middleware 3 DOWN' );
yield next;
console.log( 'middleware 3 UP' );
});
app.use(function * (next) {
console.log( 'middleware 4 DOWN' );
});
app.use(function * (next) {
// This middleware will not get reached
// since the previous middleware did not
// yield 'next'
console.log( 'middleware 5 DOWN' );
});
app.listen(5000);
http.get('http://localhost:5000', function() { process.exit(1); });
// => middleware 1 DOWN
// => middleware 2 DOWN
// => middleware 3 DOWN
// => middleware 4 DOWN
// => middleware 3 UP
// => middleware 2 UP
// => middleware 1 UP