KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架。可以直接在项目里使用 ES6/7(Generator Function, Class, Async & Await)等特性,借助 Babel 编译,可稳定运行在 Node.js 环境上。

介绍

KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架。可以直接在项目里使用 ES6/7(Generator Function, Class, Async & Await)等特性,借助 Babel 编译,可稳定运行在 Node.js 环境上。

//base controller, admin/controller/base.controller.js 
export default class extends koahub.http{
 
    constructor() {
        super();
        console.log('base constructor');
    }
 
    isLogin() {
        console.log('base isLogin');
    }
}
 
//index controller, admin/controller/index.controller.js 
import base from "./base.controller";
export default class extends base{
 
    constructor() {
        super();
        console.log('index constructor');
    }
 
    index() {
        super.isLogin();
        super.json({msg: 'this is a msg'});
        console.log('index index');
    }
}

项目中可以使用 ES6/7 里的所有特性,借助 Babel 编译,可以稳定运行在 >=0.12.0 的 Node.js 环境中。

组件1: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\//]
 }
 ]);
 

官网:http://js.koahub.com

组件2: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);
}));

官网:http://js.koahub.com

KoaHub.js:使用ES6/7特性开发Node.js框架的更多相关文章

  1. KoaHub.js:使用ES6/7特性开发Node.js框架(2)

    介绍   KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async ...

  2. [Node.js] Using ES6 and beyond with Node.js

    If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...

  3. Atitit js es5 es6新特性 attilax总结

    Atitit js es5 es6新特性 attilax总结 1.1. JavaScript发展时间轴:1 1.2. 以下是ES6排名前十的最佳特性列表(排名不分先后):1 1.3. Es6 支持情况 ...

  4. VS轻松开发Node.js应用

    PTVS开发团队又开发出一款可以在VS里编写Node.js应用程序的插件--NTVS(Node.js Tools for Visual Studio),开发者可以在VS里轻松开发Node.js应用. ...

  5. 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure

    [题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在 ...

  6. 在Visual Studio上开发Node.js程序

    [题外话] 最近准备用Node.js做些东西,于是找找看能否有Visual Studio上的插件以方便开发.结果还真找到了一个,来自微软的Node.js Tools for Visual Studio ...

  7. 【转】使用VS开发 Node.js指南

    参考:https://www.visualstudio.com/features/node-js-vs 这篇文章主要介绍了使用VS开发 Node.js的方法,主要是使用NTVS(Node.js Too ...

  8. 在开发node.js中,关于使用VS2013插件出现一直读取资源的问题

    情况描述: 1.安装了VS2013: 2.安装了VS开发node.js的插件; 3.打开以前的工程文件,有的可以打开,有的打不开.而且打不开的始终停留在读取资源的界面.很痛苦的.等半天都没有反应.到底 ...

  9. 在Visual Studio 2013 上开发Node.js程序

    [题外话] 最近准备用Node.js做些东西,于是找找看能否有Visual Studio上的插件以方便开发.结果还真找到了一个,来自微软的Node.js Tools for Visual Studio ...

随机推荐

  1. word中利用宏替换标点标点全角与半角

    Alt+F11,然后插入-模块: 复制下面代码到编辑窗口: Sub 半角标点符号转换为全角标点符号() '中英互译文档中将中文段落中的英文标点符号替换为中文标点符号 Dim i As Paragrap ...

  2. 用Zephir编写PHP扩展

    自从NodeJS,和Golang出来后,很多人都投奔过去了.不为什么,冲着那牛X的性能.那PHP的性能什么时候能提升一下呢?要不然就会被人鄙视了.其实大牛们也深刻体会到了这些威胁,于是都在秘密开发各种 ...

  3. storm遇到问题汇总

    http://www.reader8.cn/jiaocheng/20131023/2139887.html 错误1:在windows下运行ExclamationTopology或者WordCountT ...

  4. 005.数组、for、foreach

    1.方法的传输传递 值参数:传递的是副本 引用参数:自身 保留自定义的方法中对值的改变 形参影响实参ref:对应的形参和实参都用ref修饰 输出参数:实参不用赋值,但是自定义方法内必须对此参数赋值!! ...

  5. HDU1175(dfs)

    连连看 Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  6. Python实现字典的key和values的交换

    #encoding=utf-8 #反转字典 即key和val互换 dict1={1:2,3:4,6:7,9:10} print '---生成器表达式' def invert_dict(d): retu ...

  7. yii2 中布局文件的 设置方法

    网页主题应用的属性: [yii\base\Application::layout|layout 该属性指定渲染 视图 默认使用的布局名字,默认值为 'main' 对应布局路径下的 main.php 文 ...

  8. 如何给 UILable 添加横线

    类似淘宝上的原价现价,原价上一般都会有一条横线,这种效果怎么实现呢?其实相当的简单,我们只需要重写自定义的lable的 - (void)drawRect:(CGRect)rect 方法就行了. 具体实 ...

  9. HDU 3782 xxx定律

    xxx定律 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. 深入理解DOM事件类型系列第六篇——加载事件

    前面的话 提到加载事件,可能想到了window.onload,但实际上,加载事件是一大类事件,本文将详细介绍加载事件 load load事件是最常用的一个事件,当页面完全加载后(包括所有图像.java ...