koa-hornbill-fetch

1.0.2 • Public • Published

koa-hornbill-fetch

KOA-hornbill绑定数据的中间件

Install

$ npm install koa-hornbill-fetch --save

Usage

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!');
});

Test

npm test

License

MIT

Package Sidebar

Install

npm i koa-hornbill-fetch

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • xiongwilee