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

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

时间: 2024-12-18 05:46:22

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

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

    }       isLogin() {         console.log('base isLogin');     } }   //index controller, admin/controller/index.controller.js import base from "./base.controller"; export default class extends base{       constructor() {         super();      

VS轻松开发Node.js应用

PTVS开发团队又开发出一款可以在VS里编写Node.js应用程序的插件--NTVS(Node.js Tools for Visual Studio),开发者可以在VS里轻松开发Node.js应用. NTVS是一款开源工具,遵循Apache开源许可,由微软和社区维护.适用于Node.js 0.10.20版或更高的版本上.NTVS具有可编辑.智能提示.分析.NPM.调式(本地和远程)等功能,并且还可以发布在Azure网站和Cloud服务上. Node.js可在32位和64位架构上运行,NTVS也同

intellij IDEA开发node.js

现在网上好像关于IDEA开发node.js的讲解不是太多,今天试了一下,竟然成功了.... 1.安装nodejs http://nodejs.org/download/  自动根据系统下载自己的版本node.js 2.环境变量 windows 安装,不需要配置环境变量   mac安装后,会提示你配置环境变量,需要配置环境变量(貌似) 命令  sudo vim /etc/paths 输入密码 然后将路径输入进去, 3.测试是否成功 控制台输入node -v 或者输入node 进去node模式然后输

使用WebStorm和Git开发Node.js应用

使用WebStorm和Git开发Node.js应用 一.使用WebStorm创建express项目1.创建项目File|new project,先选择location为D:\,输入项目名称Caller,选择Node.js Express项目.2.修改配置:智能感知file|setting|javascript|node.js,在project目录下只选择三个Ecma script,也就是js支持,再选择两个node.js支持.移动到public目录,开启dhtml.html5.注意每行右侧是可多

在Visual Studio上开发Node.js程序

[题外话] 最近准备用Node.js做些东西,于是找找看能否有Visual Studio上的插件以方便开发.结果还真找到了一个,来自微软的Node.js Tools for Visual Studio(NTVS),虽然现在仅发布了1.0 Alpha版本,但使用起来已经非常方便.而且,其开发团队与Python Tools for Visual Studio(PTVS)是同一个,而PTVS就是Visual Studio 2013中要创建自带的Python项目需要安装的那个程序,所以大可放心的使用NT

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

[题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在其他机器上运行程序,进而需要远程调试功能,不过还好,NTVS提供的远程调试也非常方便. [系列索引] 在Visual Studio上开发Node.js程序——NTVS介绍及使用 在Visual Studio上开发Node.js程序(2)——NTVS远程调试及发布到Azure [文章索引] NTVS远

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

[题外话] 最近准备用Node.js做些东西,于是找找看能否有Visual Studio上的插件以方便开发.结果还真找到了一个,来自微软的Node.js Tools for Visual Studio(NTVS),虽然现在仅发布了1.0 Alpha版本,但使用起来已经非常方便.而且,其开发团队与Python Tools for Visual Studio(PTVS)是同一个,而PTVS就是Visual Studio 2013中要创建自带的Python项目需要安装的那个程序,所以大可放心的使用NT

开发Node.js应用最佳IDE Top 10(下)

在开发Node.js应用最佳IDE Top 10(上)一文中,我们为大家介绍了排名前5的开发Node.js应用最佳的IDE,有Cloud 9.Intellij IDEA.WebStorm.Komodo.Koding这五个最受欢迎的IDE.今天,我们将继续为大家揭晓余下的排名,一起来看看都有哪些IDE榜上有名吧! 6) Eclipse Eclipse是一个使用Java. PHP.C++等开发Web的基于云的流行IDE.您可以使用Node.js插件轻松地利用Eclipse IDE的nodeclips

详细说明 配置 Sublime Text 开发node.js(windows)包括sub2和sub3的区别

1.先安装Sublime Text  2或者3皆可 2.下载 sublime Text 的nodejs插件 得到那个zip包(后面会介绍用Package Control安装) 3.下载后解压 直接改名为nodejs(一定是这个名字,否则sub2里编辑default文件会报错) 放到 Preferences-->浏览程序包Browse Packages所在的文件夹. 4.打开刚才的nodejs文件夹 找到根目录里面的 Nodejs.sublime-build文件 如下 { "cmd"