代码拉取完成,页面将自动刷新
同步操作将从 koahubjs/koahub 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
KoaHub.js -- 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件,可以直接在项目里使用 ES6/7(Generator Function, Class, Async & Await)等特性,借助 Babel 编译,可稳定运行在 Node.js 环境上。
//base controller, admin/controller/base.controller.js
export default class extends koahub.controller {
async _initialize() {
console.log('base _initialize');
}
async isLogin() {
console.log('base isLogin');
}
}
//index controller, admin/controller/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');
}
}
项目中可以使用 ES6/7 里的所有特性,借助 Babel 编译,可以稳定运行在 >= 4 的 Node.js 环境中。
npm install github:koahubjs/koahub --save
// app/index.js启动文件
import Koahub from "koahub";
// 初始化项目
const app = new Koahub();
// 启动项目
app.run();
ctx上的函数或参数将自动加载到Controller,例如支持 this.body = 'Hello World!'
, ctx中具体的API请参考Koa.js, Controller中的扩展方法如下。
this.ctx;
this.next;
this.isGet();
this.isPost();
this.isAjax();
this.isPjax();
this.isMethod(method);
this.hook.add(name, action);
await this.hook.run(name, ...args);
this.download(file);
this.view(data);
this.json(data, msg, code);
this.success(data, msg);
this.error(data, msg);
await this.action(path, ...args);
app.use(async function (ctx, next) {
ctx.model = function() {
// ....
}
await next();
});
// 控制器中可以直接通过this.model调用
// app/config/default.config.js
export default {
port: 3000,
default_module: 'admin'
}
//默认配置如下
//启动端口
port: 3000,
//默认模块,控制器,操作
default_module: 'home',
default_controller: 'index',
default_action: 'index',
//favicon设置
favicon: 'www/favicon.ico',
//http日志
logger: true,
//url后缀
url_suffix: '',
//body配置
body: {
multipart: true
},
//cors配置
cors: false,
//session配置
session: false,
//static配置
static: false,
//自动加载配置
loader: {
"controllers": [{
root: 'controller',
suffix: '.controller.js',
prefix: '/',
}, {
root: 'addon',
suffix: '.controller.js',
prefix: '/addon/',
filter: [/\/controller/]
}],
"configs": {
root: 'config',
suffix: '.config.js'
}
}
// 控制器初始化,前置,后置,空操作
async _initialize()
async _before()
async _before_index()
async index()
async _after_index()
async _after()
async _empty()
// 控制器私有方法
// 方法首页字符是`_`为私有方法
// 支持restful路由设置
// app/config/router.config.js
export default [
['/product', {
get: "/home/product/index"
}],
['/product/:id', {
get: "/home/product/detail",
post: "/home/product/add",
put: "/home/product/update",
delete: "/home/product/delete",
}]
]
// 下载demo
git clone https://github.com/koahubjs/koahub-demo.git
// 进入项目
cd koahub-demo
// 安装依赖
npm install
// 启动项目
npm start
// 下载demo
git clone https://github.com/koahubjs/koahub-demo-promise.git
// 进入项目
cd koahub-demo-promise
// 安装依赖
npm install
// 启动项目
npm start
// 下载demo
git clone https://github.com/koahubjs/koahub-demo-generator.git
// 进入项目
cd koahub-demo-generator
// 安装依赖
npm install
// 启动项目
npm start
[2016-11-28 09:56:03] [Koahub] Koahub Version: 2.0.1
[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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。