KoaHub.jsNode.js Web 快速開(kāi)發(fā)框架
KoaHub.js -- 中文最佳實(shí)踐Node.js Web快速開(kāi)發(fā)框架。支持Koa.js, Express.js中間件,可以直接在項(xiàng)目里使用 ES6/7(Generator Function, Class, Async & Await)等特性,借助 Babel 編譯,可穩(wěn)定運(yùn)行在 Node.js 環(huán)境上。
//base controller, app/controller/home/base.controller.js
export default class extends koahub.http {
async _initialize() {
console.log('base _initialize');
}
async isLogin() {
console.log('base isLogin');
}
}
//index controller, app/controller/home/index.controller.js
import base from "./base.controller";
export default class extends base {
async _initialize() {
await super._initialize();
}
async index() {
this.view(1);
}
async index2() {
this.json(1, 2);
}
async index3() {
await this.render('index');
}
}
項(xiàng)目中可以使用 ES6/7 里的所有特性,借助 Babel 編譯,可以穩(wěn)定運(yùn)行在 >=0.12.0 的 Node.js 環(huán)境中。
特性
-
支持koa全部中間件
-
支持使用 ES2015+ 全部特性來(lái)開(kāi)發(fā)項(xiàng)目
-
支持?jǐn)帱c(diǎn)調(diào)試 ES2015+ 項(xiàng)目
-
支持多種項(xiàng)目結(jié)構(gòu)和多種項(xiàng)目環(huán)境
-
支持多級(jí) Controller
-
支持自動(dòng)加載
-
支持鉤子機(jī)制
-
支持Socket.io
-
支持錯(cuò)誤處理
-
支持全局koahub變量
-
支持快捷方法
-
支持修改代碼,立即生效
-
...
安裝
npm install github:koahubjs/koahub --save
創(chuàng)建啟動(dòng)文件
// app/index.js啟動(dòng)文件 import Koahub from "koahub"; //默認(rèn)app是項(xiàng)目目錄 const app = new Koahub(); app.getKoa();獲取koa實(shí)例化,支持自定義koa中間件 app.getServer();獲取server實(shí)例化,支持socket.io app.run();
方法
this.ctx; this.next; this.method(); this.isGet(); this.isPost(); this.isAjax(); this.isPjax(); this.isMethod(method); this.ip(); this.header(name, value); this.status(code); this.get(name, value); this.post(name, value);//需中間件,且快捷方法 this.file(name, value);//需中間件,且快捷方法 this.session(name, value);//需session中間件 this.cookie.get(name, options); this.cookie.set(name, value, options); this.hook.add(name, action); await this.hook.run(name, ...args); this.host(); this.redirect(url); this.download(file); this.view(data); this.json(data, msg, code); this.success(data, msg); this.error(data, msg); this.state(name, value); await this.render(tpl, locals);//需中間件 await this.action(path, ...args);
快捷中間件
// use koa-better-body 自定義post/file中間件
koa.use(async function (ctx, next) {
if (ctx.request.fields) {
ctx.post = ctx.request.fields;
}
if (ctx.request.files) {
ctx.file = ctx.request.files;
}
await next();
});
目錄結(jié)構(gòu)
|-- app | |-- addon | | |-- demo | | | |-- config.json | | | |-- controller | | | |-- model | | | `-- view | |-- config | | |-- index.config.js | |-- controller | | |-- home | | | |-- index.controller.js | | | `-- base.controller.js | | |-- admin | |-- data | |-- model | |-- util | |-- index.js |-- logs |-- node_modules |-- runtime |-- www |-- app.js |-- package.json
命令行工具
koahub Usage: koahub [options] [command] Commands: start [options] [script] koahub start script --watch --compile controller [name] koahub create controller create [project] koahub create project Options: -h, --help output usage information -V, --version output the version number Examples: koahub start app/index.js --watch --compile (文件修改自動(dòng)編譯并且重啟) koahub controller home/article (自動(dòng)創(chuàng)建控制器模版) koahub create koahub-demo (自動(dòng)初始化項(xiàng)目)
配置
// app/config/index.config.js
export default {
port: 3000,
default_module: 'admin'
}
//啟動(dòng)端口
port: 3000,
//調(diào)試模式
debug: true,
//默認(rèn)模塊,控制器,操作
default_module: 'home',
default_controller: 'index',
default_action: 'index',
//favicon設(shè)置
favicon: 'www/favicon.ico',
//hook中間件
hook: true,
//http日志
logger: true,
//url后綴
url_suffix: '',
//自動(dòng)加載配置 such as koahub.utils
loader: {
"utils": {
root: 'util',
suffix: '.util.js'
}
}
開(kāi)始應(yīng)用
git clone https://github.com/koahubjs/koahub-demo.git cd koahub-demo npm install npm start
啟動(dòng)信息:
[2016-11-28 09:56:03] [Koahub] Koahub version: 1.1.0 [2016-11-28 09:56:03] [Koahub] Koahub website: http://js.koahub.com [2016-11-28 09:56:03] [Koahub] Server Enviroment: development [2016-11-28 09:56:03] [Koahub] Server running at: http://127.0.0.1:3000
使用手冊(cè)
官網(wǎng)
評(píng)論
圖片
表情
