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. HDU-1994-利息计算

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1994 水题 题目分析 就是两种储存方式,输出所得本金加利息 代码 #include<stdio. ...

  2. 开箱即用 - Grunt合并和压缩 js,css 文件

    js,css 文件合并与压缩 Grunt 是前端自动化构建工具,类似webpack. 它究竟有多强悍,请看它的 介绍. 这里只演示如何用它的皮毛功能:文件合并与压缩. 首先说下js,css 合并与压缩 ...

  3. 如何利用jquery 实现表格数据的搜索功能

    在表格的操作中,常常会遇到通过关键字来搜索结果,这个功能用jquery的filter实现非常简单. 我以一个小例子说明: <table> <thead> <tr cols ...

  4. Javascript日期格式化指定格式的字符串实现

    代码部分 TypeScript /** * format a Date object * 将 Date 转化为指定格式的String * @param {Date} date 源日期对象 * @par ...

  5. 自学javaee程序员之路--ssm的小项目(一)

    大家好~我叫王聪,缩写是WC(不是厕所!不是厕所!).是一名某内陆大四的学生.这两个月自学了javaee---关于web的一些心得,分享记录一下.建立这个博客的目的是望各位前辈学长指正批评~~也是建立 ...

  6. 【小梅哥FPGA进阶教程】第九章 基于串口猎人软件的串口示波器

    九.基于串口猎人软件的串口示波器 1.实验介绍 本实验,为芯航线开发板的综合实验,该实验利用芯航线开发板上的ADC.独立按键.UART等外设,搭建了一个具备丰富功能的数据采集卡,芯航线开发板负责进行数 ...

  7. Java字符串之String与StringBuilder

    String与SringBuiler的一些比较   在Java中,我们会大量使用字符串,但是String究竟是怎样工作的我们可能没有想过太多,其实在String类中,每一个看起来会修改String值的 ...

  8. 下一个项目为什么要用 SLF4J

    阿里巴巴 Java 开发手册 前几天阿里巴巴在云栖社区首次公开阿里官方Java代码规范标准,就是一个PDF手册,有命名规范,让你知道自己原来取的每一个类名.变量名都是烂名字,真替你家未来孩子担心:有集 ...

  9. Android 如何保证service在后台不被kill

    Android 其实无法做多绝对的不被后台kill掉,我们只能尽量使用一些操作提升不被kill的机会. 一.onStartCommand方法,返回START_STICKY   START_STICKY ...

  10. iOS程序生命周期 AppDelegate

    iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受 ...