Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: egg and koa #179

Merged
merged 6 commits into from
Jan 7, 2017
Merged

docs: egg and koa #179

merged 6 commits into from
Jan 7, 2017

Conversation

dead-horse
Copy link
Member

No description provided.

@codecov-io
Copy link

codecov-io commented Jan 7, 2017

Current coverage is 97.65% (diff: 100%)

Merging #179 into master will not change coverage

@@             master       #179   diff @@
==========================================
  Files            33         33          
  Lines           853        853          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
  Hits            833        833          
  Misses           20         20          
  Partials          0          0          

Powered by Codecov. Last update 51fd48c...ec78106

This was referenced Jan 7, 2017
@dead-horse dead-horse changed the title docs: egg and koa(framework) docs: egg and koa Jan 7, 2017
@dead-horse dead-horse removed the WIP label Jan 7, 2017

因此社区提供了各种异步的解决方案,最终胜出的是 Promise,它也内置到了 ECMAScript 2015 中。而在 Promise 的基础上,结合 Generator 提供的切换上下文能力,出现了 [co](https://github.com/tj/co) 等第三方类库来让开发者用同步写法编写异步代码。同时,[async await](https://github.com/tc39/ecmascript-asyncawait) 这个官方解决方案也已经定稿,将于 ECMAScript 2017 中发布。

### Generator 和 Co
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

co 小写


### Async Await

Async Await 的原理其实和 co 类似,但是是语言层面提供的语法糖,通过 Async Await 编写的代码和 co + generator 编写的代码看起来很类似:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

它是语言层面提供的语法糖


### Extend

在基于 egg 的框架或者应用中,我们可以通过定义 `app/extend/{application,context,request,response}.js` 来扩展 koa 中对应的四个对象的原型,通过这个功能,我们可以快速的增加更多的辅助方法,例如我们在 `app/extend/context.js` 中写入下列代码:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一个链接到 basics/extend.md ?


相较于 co 支持的种类,async await 不能直接 await 一个 `Promise` 数组(可以通过 `Promise.all` 来封装),也不能 await `thunk`。

由于 async await 还尚未随着规范发布,node 7 中带的 v8 版本已经支持(但是仍然有问题),async await 还不能直接使用,必须经过 babel 等模块进行编译。同时, koa 2 开始支持 `Async Function` 类型的中间件。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V8


由于 async await 还尚未随着规范发布,node 7 中带的 v8 版本已经支持(但是仍然有问题),async await 还不能直接使用,必须经过 babel 等模块进行编译。同时, koa 2 开始支持 `Async Function` 类型的中间件。

## Koa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

koa


> Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.

koa 和 express 的设计风格非常类似,底层也都是共用的[同一套底层库](https://github.com/jshttp),但是有几个显著的区别,除了上面提到的默认异步解决方案之外,主要的特点还有下面几个:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

。句号

Copy link
Member

@popomore popomore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


### Midlleware

koa 的中间件和 express 不同,是类似洋葱圈形式的:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

koa 的中间件和 express 不同,是类似洋葱圈形式的:

koa 的中间件和 express 中间件不同的地方在于 koa 选择洋葱模型


![](https://raw.githubusercontent.com/koajs/koa/master/docs/middleware.gif)

所有的请求经过一个中间件的时候都会执行两次,对比 express 形式的 middleware,koa 的模型可以非常方便的实现后置处理逻辑,对比 koa 和 express 的 compress 中间件就可以明显的感受到优势。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对比 express 形式的 middleware

对比 express 形式的中间件

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

koa 的模型可以非常方便的实现后置处理逻辑,对比 koa 和 express 的 compress 中间件就可以明显的感受到优势。

koa 的中间件模型可以非常方便的实现后置处理逻辑,对比 koa 和 express 的 compress 中间件就可以明显的感受到优势。

所有的请求经过一个中间件的时候都会执行两次,对比 express 形式的 middleware,koa 的模型可以非常方便的实现后置处理逻辑,对比 koa 和 express 的 compress 中间件就可以明显的感受到优势。

- [koa-compress](https://github.com/koajs/compress/blob/master/index.js) for koa.
- [compression](https://github.com/expressjs/compression/blob/master/index.js) for express.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只列出代码地址,感受不到,应该将2个代码核心部分发出来对比

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码太多,没法贴,简单的例子没有说服力


### Context

和 express 只有 request 和 response 两个对象不同,koa 增加了一个 Context 的对象,作为这次请求的上下文对象(在 koa 1 中为中间件的 `this`,在 koa 2 中作为中间件的第一个参数传入)。我们可以将一次请求相关的上下文都挂载到这个对象上。类似 traceId 这种需要贯穿整个请求(在后续任何第一个地方进行其他调用都需要用到)的属性就可以挂载上去。相较于 request 和 response 而言,更加符合语义。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[traceId](https://github.com/eggjs/egg-tracer/blob/master/lib/tracer.js#L12)

}
```

只需要将这个中间件放在其他中间件之前,就可以捕获它们所有的同步或者异步代码中抛出的异常了。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只需要将这个中间件放在其他中间件之前

只需要将这个中间件放在中间件列表最前面


只需要将这个中间件放在其他中间件之前,就可以捕获它们所有的同步或者异步代码中抛出的异常了。

## Egg 扩展 Koa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

egg

? 'Your operating system is iOS.'
: 'Your operating system is not iOS.';
};
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一段:更多关于扩展的内容,请查看[框架扩展](./extend.md)


在 controller 中,我们就可以使用到刚才定义的这个便捷属性了:

```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js


一个插件可以包含:

- extend: 扩展基础对象的上下文,提供各种工具类、属性。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了空格


### Plugin

总所周知,在 express 和 koa 中,经常会引入许许多多的中间件来提供各种各样的功能,例如引入 koa-session 提供 session 的支持,引入 koa-bodyparser 来解析请求 body。而 egg 提供了一个更加强大的插件机制,让这些独立领域的功能模块可以更加容易编写。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

koa-session 要外联


尽管现在 koa 2 已经比较稳定了,但是 koa 2 需要配合 async await 使用才有意义,所以仍然需要 babel 等工具进行编译。所以 egg 1 是基于 koa 1 开发的,主要考虑到:

1. 后端代码需要足够的稳定,并且出现故障时可以最快的定位到问题,所以尽量不希望让编译后的后端代码运行在生产环境。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

服务端代码

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fengmk2 fengmk2 merged commit 119c26c into master Jan 7, 2017
@fengmk2 fengmk2 deleted the docs-koa branch January 7, 2017 15:51

### async await

async await 的原理其实和 co 类似,单它是语言层面提供的语法糖,通过 async await 编写的代码和 co + generator 编写的代码看起来很类似。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->


只需要将这个中间件放在其他中间件之前,就可以捕获它们所有的同步或者异步代码中抛出的异常了。

## egg 是 koa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

egg 基于 koa

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改掉了的


- extend:扩展基础对象的上下文,提供各种工具类、属性。
- middleware:增加一个或多个中间件,提供请求的前置、后置处理逻辑。
- config:配置各个环境下插件自身的默认配置项。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不介绍那么细节吧,这里还没介绍到什么是 service 呢


尽管现在 koa 2 已经比较稳定了,但是 koa 2 需要配合 async await 使用才有意义,所以仍然需要 babel 等工具进行编译。所以 egg 1 是基于 koa 1 开发的,主要考虑到

1. 服务端代码需要足够的稳定,并且出现故障时可以最快的定位到问题,所以尽量不要让编译后的后端代码运行在生产环境。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1. -> -

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要改成这个?现在这样不好?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

都可以吧, 只是觉得没啥顺序的话, - 更好点, 看到很多行 1. 有强迫症

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants