主题:KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架
KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架
koahub
koahub
koahub web fast framework
Installation
$ npm install koahub
Use with koa
var app = require('koa')();
var router = require('koa-router')();
require('koahub');
for (var key in koahub.controller){
router.use(key, koahub.controller[key]);
}
// sample as
router.use('/home', auth('home').skip({path: [/public/]}));
app.use(router.routes());
// sample as
console.log(koahub.model);
console.log(koahub.controller);
console.log(koahub.util);
app.listen(3000);
koahub-loader
koahub-loader
koahub loader
Installation
$ npm install koahub-loader
Use with koa
// 1.model loader
var model = loader([
{
root: './app/model',
suffix: '.model.js'
},
{
root: './addon',
suffix: '.model.js',
filter: [/\w*\/model\//]
}
]);
// 2.controller loader
var app = require('koa')();
var router = require('koa-router')();
var controller = loader([
{
root: './app/controller',
suffix: '.controller.js',
prefix: '/',
}, {
root: './addon',
suffix: '.controller.js',
prefix: '/addon/',
filter: [/\w*\/controller\//]
}
]);
for (var key in controller) {
router.use(key, controller[key].routes());
}
app.use(router.routes());
// 3.util loader
var util = loader([
{
root: './app/common',
suffix: '.util.js'
},
{
root: './addon',
suffix: '.util.js',
filter: [/\w*\/common\//]
}
]);
koahub-skip
koahub skip middleware
koahub skip
Conditionally skip a middleware when a condition is met.
Install
npm i koahub-skip --save
Usage
With existing middlewares:
var skip = require('koahub-skip');
var serve = require('koa-static');
var static = serve(__dirname + '/public');
static.skip = skip;
app.use(static.skip({ method: 'OPTIONS' }));
If you are authoring a middleware you can support skip as follow:
module.exports = function () {
var mymid = function *(next) {
// Do something
};
mymid.skip = require('koahub-skip');
return mymid;
};
Current options
· method it could be an string or an array of strings. If the request method match the middleware will not run.
· path it could be an string, a regexp or an array of any of those. If the request path match, the middleware will not run.
· ext it could be an string or an array of strings. If the request path ends with one of these extensions the middleware will not run.
· custom it must be a function that returns true / false. If the function returns true for the given request, ithe middleware will not run. The function will have access to Koa's context via this
· useOriginalUrl it should be true or false, default is true. if false, path will match against ctx.url instead of ctx.originalUrl.
Examples
Require authentication for every request skip the path is index.html.
app.use(requiresAuth().skip({ path: ['/index.html', '/'] }))
Avoid a fstat for request to routes doesnt end with a given extension.
app.use(static.skip(function () {
var ext = url.parse(this.originalUrl).pathname.substr(-4);
return !~['.jpg', '.html', '.css', '.js'].indexOf(ext);
}));
koahub-body-res
koahub body res
Format koa's respond json.
Installation
$ npm install koahub-body-res
Use with koa
var app = require('koa')();
var koaRes = require('koahub-body-res');
app.use(koaRes());
this.data = 'This is a body';
this.msg = 'This is a msg';
output
this.body = {
code: 200,
data: 'This is a body',
msg: 'This is a msg'
}
koahub-yilianyun
微信易联云打印机接口
koahub-yilianyun易联云打印机node接口
Installation
$ npm install koahub-yilianyun
Use with co-request
var printer = require('koahub-yilianyun');
var result = yield printer({
"partner": 914,//用户id(管理中心系统集成里获取)
"apikey": "3785b31b2c84f3c47e51a6c4481f8a5fc2eea72a",//apikey(管理中心系统集成里获取)
"machine_code": "3400453726",//打印机终端号
"msign": "gn5p5zk585b6",//打印机终端密钥
"time": parseInt(new Date().getTime() / 1000),//当前时间戳(服务器用于验证超时)
"content": '2222'//需要传输打印的内容数据
});
KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架