- 刘博文 @奇舞团
- 95后
- https://github.com/berwin
var express = require('express');
var app = express();
app.use(function (req, res, next) {
// ...
next();
})
app.use(function (req, res) {
res.send('Hello World!');
});
app.listen(3000);
req, res, next
var koa = require('koa');
var app = koa();
// logger
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
// response
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);
this, yield, next
var app = koa();
var koa = require('koa');
var app = koa();
// x-response-time
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
});
// logger
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
// response
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);
var koa = require('koa');
var app = koa();
app.use(function* (next) {
console.log('f1: pre next');
yield next;
console.log('f1: post next');
});
app.use(function* (next) {
console.log(' f2: pre next');
yield next;
console.log(' f2: post next');
});
app.use(function* (next) {
console.log(' f3: pre next');
yield next;
console.log(' f3: post next');
});
app.use(function* (next) {
console.log('hello world')
this.body = 'hello world';
});
app.listen(3000);
var koa = require('koa');
var app = koa();
app.use(function *(){
console.log(this);
this.body = 'Hello World';
});
app.listen(3000);
var koa = require('koa');
var app = koa();
app.use(function *(next){
try {
yield next;
} catch (err) {
this.status = err.status || 500;
this.body = 'Something exploded';
}
});
app.use(function *(){
throw new Error('boom boom');
});
app.listen(3000);
var koa = require('koa');
var app = koa();
app.use(function *(){
var userID = this.query.userID;
try {
this.body = yield user.findById(userID);
} catch (err) {
this.status = err.status || 403;
this.body = 'incorrect parameter';
this.app.emit('error', err, this);
}
});
app.listen(3000);
var koa = require('koa');
var app = koa();
app.use(function *(){
function delay(interval) {
return function (done) {
setTimeout(function () {
done(null, 'delay done');
}, interval);
};
}
var time = Date.now();
console.log('收到请求');
var body = yield delay(5000);
console.log(Date.now() - time);
this.body = body;
});
app.listen(3000);
app.use(function *(){
// 先获取购物车中的数据列表
var cart = yield cart.findByUserId(userID);
// 在获取购物车中第一个物品的详细信息
this.body = yield commodity.findById(cart[0].id);
});
app.use(function *(){
var userInfo = cart.findByUserId(userID);
var cart = cart.findByUserId(userID);
this.body = yield {userInfo: userInfo, cart: cart};
});
this.body = yield [userInfo, cart];
处理异步(与async有点像)
koa充分利用co特性,通过3个步骤实现中间件逻辑
new Promise(function(resolve, reject) {
// 我是中间件1
yield new Promise(function(resolve, reject) {
// 我是中间件2
yield new Promise(function(resolve, reject) {
// 我是中间件3
yield new Promise(function(resolve, reject) {
// 我是body
});
// 我是中间件3
});
// 我是中间件2
});
// 我是中间件1
});
function *() {
yield *m1(m2(m3(noop())))
}
app.use(function* f1(next) {
console.log('f1: pre next');
yield next;
console.log('f1: post next');
});
app.use(function* f2(next) {
console.log(' f2: pre next');
yield next;
console.log(' f2: post next');
});
app.use(function* f3(next) {
console.log(' f3: pre next');
yield next;
console.log(' f3: post next');
});
app.use(function* (next) {
this.body = 'hello world';
});
function co(gen) {
return new Promise(function(resolve, reject) {
onFulfilled();
function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
function next(ret) {
if (ret.done) return resolve(ret.value);
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
}
});
}
var koa = require('koa');
var app = koa();
app.use(function* f1(next) {
console.log('f1: pre next');
yield next;
console.log('f1: post next');
yield next;
console.log('f1: done');
});
app.use(function* f2(next) {
console.log(' f2: pre next');
yield next;
console.log(' f2: post next');
yield next;
console.log(' f2: done');
});
app.use(function* f3(next) {
console.log(' f3: pre next');
yield next;
console.log(' f3: post next');
yield next;
console.log(' f3: done');
});
app.use(function* (next) {
console.log('hello world')
this.body = 'hello world';
});
app.listen(3000);
function(req, res){
res.statusCode = 404;
var ctx = self.createContext(req, res);
fn.call(ctx).then(function () {
respond.call(ctx);
}).catch(ctx.onerror);
}
/**
* Initialize a new context.
*
* @api private
*/
app.createContext = function(req, res){
var context = Object.create(this.context);
var request = context.request = Object.create(this.request);
var response = context.response = Object.create(this.response);
context.app = request.app = response.app = this;
context.req = request.req = response.req = req;
context.res = request.res = response.res = res;
request.ctx = response.ctx = context;
request.response = response;
response.request = request;
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys);
context.accept = request.accept = accepts(req);
context.state = {};
return context;
};