KoaHub.js- 中文最佳实践Node.js Web快速开发框架

网友投稿 548 2022-10-28

KoaHub.js- 中文最佳实践Node.js Web快速开发框架

KoaHub.js- 中文最佳实践Node.js Web快速开发框架

介绍

KoaHub.js – 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件,可以直接在项目里使用ES6/7(Generator Function, Class, Async & Await)等特性,借助 Babel 编译,可稳定运行在 Node.js环境上。

//base controller, app/controller/home/base.controller.jsexport default class extends koahub.http {    async _initialize() {        console.log('base _initialize');    }    async isLogin() {        console.log('base isLogin');    }}//index controller, app/controller/home/index.controller.jsimport 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 编译,可以稳定运行在 >=0.12.0 的 Node.js 环境中。

特性

支持koa全部中间件支持使用 ES2015+ 全部特性来开发项目支持断点调试 ES2015+ 项目支持多种项目结构和多种项目环境支持多级 Controller支持自动加载支持钩子机制支持Socket.io支持错误处理支持全局koahub变量支持快捷方法支持修改代码,立即生效…

安装

npm install github:koahubjs/koahub --save

创建启动文件

// app/index.js启动文件import Koahub from "koahub";//默认app是项目目录const app = new Koahub();app.getKoa();获取koa实例化,支持自定义koa中间件app.getServer();获取server实例化,支持socket.ioapp.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();});

目录结构

|-- 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

命令行工具

koahubUsage: koahub [options] [command]Commands:start [options] [script]  koahub start script --watch --compilecontroller [name]         koahub create controllercreate [project]          koahub create projectOptions:-h, --help     output usage information-V, --version  output the version numberExamples:koahub start app/index.js --watch --compile (文件修改自动编译并且重启)koahub controller home/article (自动创建控制器模版)koahub create koahub-demo (自动初始化项目)

配置

// app/config/index.config.jsexport default {    port: 3000,    default_module: 'admin'}//启动端口port: 3000,//调试模式debug: true,//默认模块,控制器,操作default_module: 'home',default_controller: 'index',default_action: 'index',//favicon设置favicon: 'www/favicon.ico',//hook中间件hook: true,//http日志logger: true,//url后缀url_suffix: '',//自动加载配置 such as koahub.utilsloader: {    "utils": {        root: 'util',        suffix: '.util.js'    }}

开始应用

git clone https://github.com/koahubjs/koahub-demo.gitcd koahub-demonpm installnpm start

启动信息:

[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

使用手册

KoaHub.js手册

官网

KoaHub.js官网

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:wego- 采用 go-kit 模式封装的 golang 开发框架
下一篇:基于fastify框架所写的api接口调用服务
相关文章

 发表评论

暂时没有评论,来抢沙发吧~