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. EditText的功能与用法

    EditText与TextView非常相似,它甚至与TextView共用了绝大部分XML属性和方法.EditText和TextView的最大区别在于:EditText可以接受用户输入. EditTex ...

  2. globalToLocal的坐标变换

    globalToLocal $(function() { init(); }); // globalToLocal var stage, holder1, holder2,shape; functio ...

  3. Java线程:锁

    一.锁的原理 Java中每个对象都有一个内置锁,当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行的代码类的当前实例(this实例)有关的锁.获得一个对象的锁也称为获取锁.锁 ...

  4. Cookie的一些用法

    Cookie的一些用法: package com.stono.servlet.listenerorder; import java.io.IOException; import java.io.Pri ...

  5. RestTemplate.getForObject返回List的时候处理方式

    ...... User[] users = restTemplate.getForObject(url, User[].class); ......

  6. 老司机教你下载tumblr上视频和图片的正确姿势

    本文面向初学者. 很多同学问我:“我非常想学Python编程,但是找不到兴趣点”. 还有的同学呢,找到了很好的兴趣点,但是无从下手,“玄魂老师,我想下载tumblr上的视频, 怎么下载,Python能 ...

  7. 为苹果ATS和微信小程序搭建 Nginx + HTTPS 服务

    昨天测试开发微信小程序,才发现微信也要求用HTTPS加密数据,想来是由于之前苹果的ATS审核政策的缘故吧,微信想在苹果上开放小程序必然也只能要求开发者必须使用HTTPS了,于是在服务器上测试安装Ngi ...

  8. RxJava2出现:Unable to create call adapter for io.reactivex.Flowable

    前面一直使用的是Rxjava 1.x 版本,最近 Rxjava 2.x 版本发布了,并且支持了背压,便换成了 Rxjava 2.x 版本.更换之后出现了下面的错误. Caused by: java.l ...

  9. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

    转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...

  10. Java中boolean类型到底占用多少字节

    虽然 Java 虚拟机定义了 boolean 这种数据类型,但是只对它提供了非常有限的支持.在 Java 虚拟机中没有任何供 boolean 值专用的字节码指令,在 Java 语言之中涉及到 bool ...