Skip to content

Commit

Permalink
docs: add gitter & more controller ctx style (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Mar 16, 2017
1 parent a172960 commit 871aa82
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 105 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Born to build better enterprise frameworks and apps
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![NPM download][download-image]][download-url]
[![Gitter][gitter-image]][gitter-url]

[npm-image]: https://img.shields.io/npm/v/egg.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg
Expand All @@ -21,6 +22,8 @@ Born to build better enterprise frameworks and apps
[snyk-url]: https://snyk.io/test/npm/egg
[download-image]: https://img.shields.io/npm/dm/egg.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg
[gitter-image]: https://img.shields.io/gitter/room/eggjs/egg.svg?style=flat-square
[gitter-url]: https://gitter.im/eggjs/egg

## Installation

Expand Down
4 changes: 2 additions & 2 deletions docs/source/en/intro/egg-and-koa.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ It can be used in controller then:

```js
// app/controller/home.js
exports.handler = function*() {
this.body = this.isIOS
exports.handler = function* (ctx) {
ctx.body = this.isIOS
? 'Your operating system is iOS.'
: 'Your operating system is not iOS.';
};
Expand Down
4 changes: 2 additions & 2 deletions docs/source/zh-cn/basics/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ exports.listApp = function* (ctx) {
//
// {"title": "controller", "content": "what is controller"}
exports.listPosts = function* (ctx) {
assert.equal(this.request.body.title, 'controller');
assert.equal(this.request.body.content, 'what is controller');
assert.equal(ctx.request.body.title, 'controller');
assert.equal(ctx.request.body.content, 'what is controller');
};
```

Expand Down
45 changes: 22 additions & 23 deletions docs/source/zh-cn/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ module.exports = app => {

```js
// app/controller/user.js
exports.info = function* () {
this.body = {
name: `hello ${this.params.id}`,
exports.info = function* (ctx) {
ctx.body = {
name: `hello ${ctx.params.id}`,
};
};
```
Expand Down Expand Up @@ -62,9 +62,8 @@ app.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller
### 注意事项

- 在 Router 定义中, 可以支持多个 Middleware 串联执行
- Controller 必须定义在 `app/controller` 目录中,并且对应的函数一定要是 generator function。
- 一个文件里面也可以包含多个 Controller 定义,在定义路由的时候,
可以通过 `controller-filename.function-name` 的方式指定对应的 Controller。
- Controller 必须定义在 `app/controller` 目录中。
- 一个文件里面也可以包含多个 Controller 定义,在定义路由的时候,可以通过 `controller-filename.function-name` 的方式指定对应的 Controller。

下面是一些路由定义的方式:

Expand Down Expand Up @@ -134,8 +133,8 @@ module.exports = app => {
};

// app/controller/search.js
module.exports = function* () {
this.body = `search: ${this.query.name}`;
module.exports = function* (ctx) {
ctx.body = `search: ${this.query.name}`;
};

// curl http://127.0.0.1:7001/search?name=egg
Expand All @@ -150,8 +149,8 @@ module.exports = app => {
};

// app/controller/user.js
exports.info = function* () {
this.body = `user: ${this.params.id}, ${this.params.name}`;
exports.info = function* (ctx) {
ctx.body = `user: ${ctx.params.id}, ${ctx.params.name}`;
};

// curl http://127.0.0.1:7001/user/123/xiaoming
Expand All @@ -168,10 +167,10 @@ module.exports = app => {
};

// app/controller/package.js
exports.detail = function* () {
// 如果请求 URL 被正则匹配, 可以按照捕获分组的顺序,从 this.params 中获取。
// 按照下面的用户请求,`this.params[0]` 的 内容就是 `egg/1.0.0`
this.body = `package:${this.params[0]}`;
exports.detail = function* (ctx) {
// 如果请求 URL 被正则匹配, 可以按照捕获分组的顺序,从 ctx.params 中获取。
// 按照下面的用户请求,`ctx.params[0]` 的 内容就是 `egg/1.0.0`
ctx.body = `package:${ctx.params[0]}`;
};

// curl http://127.0.0.1:7001/package/egg/1.0.0
Expand All @@ -186,8 +185,8 @@ module.exports = app => {
};

// app/controller/form.js
module.exports = function* () {
this.body = `body: ${JSON.stringify(this.request.body)}`;
module.exports = function* (ctx) {
ctx.body = `body: ${JSON.stringify(ctx.request.body)}`;
};

// 模拟发起 post 请求。
Expand Down Expand Up @@ -230,10 +229,10 @@ const createRule = {
},
};

exports.create = function* () {
exports.create = function* (ctx) {
// 如果校验报错,会抛出异常
this.validate(createRule);
this.body = this.request.body;
ctx.validate(createRule);
ctx.body = ctx.request.body;
};

// curl -X POST http://127.0.0.1:7001/user --data 'username=abc@abc.com&password=111111&re-password=111111'
Expand All @@ -251,8 +250,8 @@ module.exports = app => {
};

// app/controller/home.js
exports.index = function* () {
this.body = 'hello controller';
exports.index = function* (ctx) {
ctx.body = 'hello controller';
};

// curl -L http://localhost:7001
Expand Down Expand Up @@ -289,8 +288,8 @@ module.exports = function* () {

```js
// app/controller/search.js
module.exports = function* () {
this.body = `search: ${this.query.name}`;
module.exports = function* (ctx) {
ctx.body = `search: ${this.query.name}`;
};

// app/middleware/uppercase.js
Expand Down
Loading

0 comments on commit 871aa82

Please sign in to comment.