nodejs 的三大特色:

nodejs

异步解决方案历史变迁

回调嵌套 -> event-pipe, async -> co -> fibjs

koa

基于co, 使用generator来使得异步代码使用起来同步化。

需求来了

  1. koa封装了大量逻辑, 只想使用类似connect的提供原始IncomingMessageServerResponse对象.

  2. 很多中间件都是采用connect中间件方式写的, 如果切换到koa, 必定要对原先的代码做修改.

node-cover

  1. 只提供基础的http server服务.

  2. 兼容connect中间件, 不兼容koa中间件, 但是可以使用ES6写的各种模块.

  3. 无缝切换现有项目, 同时使用co写同步代码.

使用方法

coffee-script v1.8

一. coffee-script的github master分支已经是1.8版本, 对es6很多语法都做了支持, 其中就包括generator.

1
2
3
4
5
6
7
# Function
( test ) ->
....

# GeneratorFuntion
( test ) ->
yield ->

二. 使用git地址安装npm包

package.json

1
2
3
4
5
6
7
8
9
10
11
12
{
.......
dependencies : {
"coffee-script" : "git+ssh://git@github.com:jashkenas/coffeescript.git"
}
}

//git://github.com/user/project.git#commit-ish
//git+ssh://user@hostname:project.git#commit-ish
//git+ssh://user@hostname/project.git#commit-ish
//git+http://user@hostname/project/blah.git#commit-ish
//git+https://user@hostname/project/blah.git#commit-ish

三. 同时, 1.8的coffee修正了一些逻辑, 偷懒的同学注意了

1
( @server ) ->

对于如上代码,1.8 版本以前的coffee会编译成

1
2
3
4
  fucntion( server ){
@server = server
.....
}

但是对于1.8版本coffee会编译成:

1
2
3
4
function( _at_server ){
@server = _at_server
.....
}

如果直接在函数中使用server变量的话,1.8版本之前是可以的,但是在1.8版本之后就会报错了。

id2