Skip to content

Commit

Permalink
docs(intro): make some changes for egg-and-koa (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunShinewyf authored and atian25 committed Nov 29, 2017
1 parent d752b3b commit a061f21
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
30 changes: 15 additions & 15 deletions docs/source/en/intro/egg-and-koa.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Node.js is an asynchronous world, asynchronous programming models in official AP
- [callback hell](http://callbackhell.com/): Notorious "callback hell"。
- [release zalgo](https://oren.github.io/blog/zalgo.html): Asynchronous functions may call callback function response data synchronously which would bring inconsistency.

The community has provided many solutions for the problems, the winner is Promise, it is built into ECMAScript 2015. On the basis of Promise, and Generator with the ability to switch context, we can write asynchronous code in synchronous way with [co] and other third party libraries. Meanwhile [async function], the official solution has been published in ECMAScript 2017 and landed in Node.js 8.
The community has provided many solutions for the problems, the winner is Promise, it is built into ECMAScript 2015. On the basis of Promise, and with the ability of Generator to switch context, we can write asynchronous code in synchronous way with [co] and other third party libraries. Meanwhile [async function], the official solution has been published in ECMAScript 2017 and landed in Node.js 8.

### Async function

[Async function] is a syntactic sugar at the language level. In async function, we can use `await` to wait a promise resolved(or rejected, which will throw an exception), and Node.js LTS (8.x) is support this feature.
[Async function] is a syntactic sugar at the language level. In async function, we can use `await` to wait for a promise to be resolved(or rejected, which will throw an exception), and Node.js LTS (8.x) has supported this feature.

```js
const fn = async function() {
Expand All @@ -27,7 +27,7 @@ fn().then(res => console.log(res)).catch(err => console.error(err.stack));

> 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.
The design style of Koa and Express are very similar, The underlying base library is the same, [HTTP library](https://github.com/jshttp). There are several significant differences between them. Besides the asynchronous solution by default metioned above, there are the following points.
The design styles of Koa and Express are very similar, The underlying basic library is the same, [HTTP library](https://github.com/jshttp). There are several significant differences between them. Besides the asynchronous solution by default mentioned above, there are the following points.

### Midlleware

Expand All @@ -41,16 +41,16 @@ The middleware in Koa is different from Express, Koa use the onion model:

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

All the requests will be executed twice during one middleware. Compared to Express middleware, it is very easy to implementing post-processing logic. You can obviously feel the advantage of Koa middleware model comparing to the compress middleware implementing in Koa and Express.
All the requests will be executed twice during one middleware. Compared to Express middleware, it is very easy to implement post-processing logic. You can obviously feel the advantage of Koa middleware model by comparing the compress middleware implementing in Koa and Express.

- [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.

### Context

Unlike that there are only two objects `Request` and `Response` in Express, Koa has one more, `Context` object in one HTTP request(it is `this` in Koa 1, while it is the first parameter for middleware function in Koa 2). We can attach all the relative things to the object. Such as [traceId](https://github.com/eggjs/egg-tracer/blob/1.0.0/lib/tracer.js#L12) that runs through the request lifetime (which will be called anywhere afterward) could be attached. It is more semantic other than request and response.
Unlike that there are only two objects `Request` and `Response` in Express, Koa has one more, `Context` object in one HTTP request(it is `this` in Koa 1, while it is the first parameter for middleware function in Koa 2). We can mount all the related properties in one request to this object. Such as [traceId](https://github.com/eggjs/egg-tracer/blob/1.0.0/lib/tracer.js#L12) that runs through the whole request lifetime (which will be called anywhere afterward) could be mounted. It is more semantic other than request and response.

At the same time Request and Response are attached to Context object. Just like Express, the two objects provide lots of easy ways to help developing. For example:
At the same time Request and Response are mounted to Context object. Just like Express, the two objects provide lots of easy ways to help developing. For example:

- `get request.query`
- `get request.hostname`
Expand All @@ -59,7 +59,7 @@ At the same time Request and Response are attached to Context object. Just like

### Exception handlering

Another enormous advantage for writing asynchronous code in synchronous way is that it is quite at ease to handler exception. You can catch all the exceptions thrown in the codes followed the convention with `try catch`. We can easily write a customized exception handlering middleware.
Another enormous advantage for writing asynchronous code in synchronous way is that it is quite at ease to handle exception. You can catch all the exceptions thrown in the codes followed the convention with `try catch`. We can easily write a customized exception handling middleware.

```js
async function onerror(ctx, next) {
Expand All @@ -73,11 +73,11 @@ async function onerror(ctx, next) {
}
```

Putting the middleware before others, you can catch all the exceptions thrown by the synchronous or asynchronous code.
Putting this middleware before others, you can catch all the exceptions thrown by the synchronous or asynchronous code.

## Egg inherit from Koa
## Egg inherits from Koa

As the above words, Koa is an excellent framework. However, it is not enough to building an enterprise-class application.
As described above, Koa is an excellent framework. However, it is not enough to build an enterprise-class application.

Egg is built around the Koa. On the basis of Koa model, Egg implements enhancements one step further.

Expand Down Expand Up @@ -111,7 +111,7 @@ More about extension, please check [Exception](../basics/extend.md) section.

### Plugin

As is known to all, Many middlewares are imported to provide different kind of features in Express and Koa. Eg, [koa-session](https://github.com/koajs/session) provides the Session support, [koa-bodyparser](https://github.com/koajs/bodyparser) help to parse request body. Egg has provided a powerful plugin mechanism to make it more easy to write stand alone features.
As is known to all, Many middlewares are imported to provide different kind of features in Express and Koa. Eg, [koa-session](https://github.com/koajs/session) provides the Session support, [koa-bodyparser](https://github.com/koajs/bodyparser) help to parse request body. Egg has provided a powerful plugin mechanism to make it easier to write stand alone features.

One plugin can include:

Expand All @@ -129,19 +129,19 @@ More about plugin, please check [Plugin](../basics/plugin.md) section.

#### Egg 1.x

When Egg 1.x released, the Node.js LTS version does not support async function,so Egg 1.x is based on Koa 1.x. On the basis of this, Egg has added full async function support. Egg is completely compatible with middlewares in Koa 2.x, all application could write with `async function`.
When Egg 1.x released, the Node.js LTS version did not support async function,so Egg 1.x was based on Koa 1.x. On the basis of this, Egg had added fully async function support. Egg is completely compatible with middlewares in Koa 2.x, all applications could write with `async function`.

- The underlying is based on Koa 1.x, asynchronous solution is based on generator function wrapped by [co].
- Official plugin and core of Egg are written in generator function, keep supporting Node.js LTS version, use [co] when necessary to be compatiable with async function.
- Application developers can choose either async function (Node.js 8.x+) or generator function (Node.js 6.x+).

#### Egg 2.x

When Node.js 8 became LTS version, async function can used in Node.js without any performance problem. Egg released 2.x based on Koa 2.x, the framework and built-in plugins are all written by async function, and Egg 2.x still keep compatibility with generator function and all the usages in Egg 1.x, applications base on Egg 1.x can migrate to Egg 2.x only by upgrade to Node.js 8.
When Node.js 8 became LTS version, async function could be used in Node.js without any performance problem. Egg released 2.x based on Koa 2.x, the framework and built-in plugins were all written by async function, and Egg 2.x still kept compatibility with generator function and all the usages in Egg 1.x, applications based on Egg 1.x can migrate to Egg 2.x only by upgrading to Node.js 8.

- The underlying will be based on Koa 2.x, asynchronous solution will be based on async function.
- Official plugin and core of egge will be written in async function.
- Recommend user transfer business layer to async function.
- Official plugin and core of egg will be written in async function.
- Recommend user to transfer business layer to async function.
- Only support Node.js 8+.

[co]: https://github.com/tj/co
Expand Down
34 changes: 17 additions & 17 deletions docs/source/en/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Quick Start

# Quick Start

This guide covers getting up and running with Egg using a real example.
This guide covers getting up and running a real example using Egg.
By following along with this guide step by step, you can quickly get started with Egg development.

## Prerequisites
Expand All @@ -13,7 +13,7 @@ By following along with this guide step by step, you can quickly get started wit

## the Quick Way

To begin with, let's quickly initialize the project using a scaffold,
To begin with, let's quickly initialize the project by using a scaffold,
which will quickly generate some of the major pieces of the application.

```bash
Expand All @@ -23,7 +23,7 @@ $ cd egg-example
$ npm i
```

Then get up and running using the following commands.
Then get up and run by using the following commands.

```bash
$ npm run dev
Expand All @@ -32,7 +32,7 @@ $ open localhost:7001

## Step by Step

Usually you could just use [egg-init] of the last session,
Usually you can just use [egg-init] of the previous section,
choose a scaffold that best fits your business model and quickly generate a project,
then get started with the development.

Expand Down Expand Up @@ -112,7 +112,7 @@ egg-example
└── package.json
```

For more infomation about directory structure, see [Directory Structure](../basics/structure.md).
For more information about directory structure, see [Directory Structure](../basics/structure.md).

Now you can start up the Web Server and see your application in action.

Expand Down Expand Up @@ -149,9 +149,9 @@ app/public
In most cases, data are usually read, processed and rendered by the templates before being presented to the user.
Thus we need to introduce corresponding template engines to handle it.

Egg does not force the use of any particular template engines,
but instead specifies the [View Plug-ins Specification](../advanced/view-plugin.md)
to allow the developers to use different plug-ins for their individual needs.
Egg does not force to use any particular template engines,
but specifies the [View Plug-ins Specification](../advanced/view-plugin.md)
to allow the developers to use different plug-ins for their individual needs instead.

For more information, cf. [View](../core/view.md).

Expand Down Expand Up @@ -185,7 +185,7 @@ exports.view = {
};
```

**Carefull! `config` dir, not `app/config`!**
**Carefully! `config` dir, not `app/config`!**

Then create a template for the index page.
This usually goes to the app/view directory.
Expand Down Expand Up @@ -247,8 +247,8 @@ You should be able to see the rendered page.

In practice, controllers usually won't generate data on their own,
neither will they contain complicated business logic.
Complicated business logic should instead be abstracted as
a busineess logic layer, i.e., [service](../basics/service.md).
Complicated business logic should be abstracted as
a busineess logic layer instead, i.e., [service](../basics/service.md).

Let's create a service to fetch data from the
[HackerNews](https://github.com/HackerNews/API).
Expand Down Expand Up @@ -320,7 +320,7 @@ exports.news = {
### Add Extensions

We might encounter a small problem here.
The time that we fetched are in Unix Time,
The time that we fetched are Unix Time format,
whereas we want to present them in a more friendly way to read.

Egg provides us with a quick way to extend its functionalities.
Expand Down Expand Up @@ -390,7 +390,7 @@ it is inevitable that we need to manage configurations.
Egg provides a powerful way to manage them in a merged configuration file.

- Environment-specific configuration files are well supported, e.g. config.local.js, config.prod.js, etc.
- Configurations could happen wherever convenient, e.g. near Applications/Plug-ins/Framesworks, and Egg will take care of merging and loading them.
- Configurations could be set wherever convenient, e.g. near Applications/Plug-ins/Framesworks, and Egg will be careful to merge and load them.
- For more information on merging, see [Configurations](../basics/config.md).

```js
Expand Down Expand Up @@ -424,9 +424,9 @@ module.exports = SomeService;

### Add Unit Testing

Unit Testing is very important, and Egg also provide [egg-bin] to help you write tests painless.
Unit Testing is very important, and Egg also provides [egg-bin] to help you write tests painless.

All the test files should place at `{app_root}/test/**/*.test.js`.
All the test files should be placed at `{app_root}/test/**/*.test.js`.

```js
// test/app/middleware/robot.test.js
Expand Down Expand Up @@ -470,9 +470,9 @@ That is all of it, for more detail, see [Unit Testing](../core/unittest.md).
## Conclusions

We can only touch the tip of the iceberg of Egg with the above short sections.
Where to go from here? Browse our documentation to better understand the framework.
Where to go from here? read our documentation to better understand the framework.
- Egg provides a powerful mechanism for extending features. See [Plugin](../basics/plugin.md).
- Egg framework allows small or large teams to work together as fast as possible under the well-documented conventions and coding best practices. In addition, the teams can build up logics on top of the framework to better suited their special needs. See more on [Frameworks].(../advanced/framework.md).
- Egg framework allows small or large teams to work together as fast as possible under the well-documented conventions and coding best practices. In addition, the teams can build up logics on top of the framework to better suit their special needs. See more on [Frameworks].(../advanced/framework.md).
- Egg framework provides code reusabilities and modularities. See details at [Progressive](../tutorials/progressive.md).
- Egg framework enables developers to write painless unit testing with many plugins and community-powered toolings. The team should give it a try by using Egg unit testing without worrying about setting up the testing tooling but writing the testing logics. See [Unit Testing](../core/test.md).

Expand Down

0 comments on commit a061f21

Please sign in to comment.