KOA-grace绑定数据的中间件
$ npm install koa-grace-fetch --save
fetch(app, options)
- app: {Object} koa instance.
- options: {Object|String->root}
- api: {String} api配置项,例如local对应http://localhost:3000,则为:api:{local:'http://localhost:3000'}
app.js
var koa = require('koa');
var fetch = require('..');
var app = koa();
// 配置api
app.use(fetch(app, {
api : {
local : 'http://127.0.0.1:3000'
}
}));
app.use(function*() {
// 数据请求
if(this.path == '/data/1'){
this.body = {
user_id:'111111'
}
return;
}else if(this.path == '/data/2'){
this.body = {
user_id:'222222'
}
return;
}
// 生成数据请求
yield this.fetch({
user1: 'local:/data/1',
user2: 'local:/data/2'
});
this.body = this.backData;
});
app.listen(3000, function() {
console.log('Listening on 3000!');
});
npm test
MIT