Skip to content

Commit

Permalink
docs(router): new style & remove app.verb (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Dec 11, 2017
1 parent 4c9eacb commit bdfd3cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
51 changes: 25 additions & 26 deletions docs/source/en/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ By unifying routing rules, routing logics are free from scatter that may cause u
```js
// app/router.js
module.exports = app => {
app.router.get('/user/:id', app.controller.user.info);
const { router, controller } = app;
router.get('/user/:id', controller.user.info);
};
```

- Implement the Controller in `app/controller` directory

```js
// app/controller/user.js
exports.info = async ctx => {
ctx.body = {
name: `hello ${ctx.params.id}`,
};
};
class UserController extends Controller {
async info() {
this.ctx.body = {
name: `hello ${ctx.params.id}`,
};
}
}
```
This simplest Router is done by now, when users do the request `GET /user/123`, the info function in `user.js` will be invoked.

Expand All @@ -33,10 +36,10 @@ This simplest Router is done by now, when users do the request `GET /user/123`,
Below is the complete definition of router, parameters can be determined depending on different scenes.

```js
router.verb('path-match', 'controller.action');
router.verb('router-name', 'path-match', 'controller.action');
router.verb('path-match', middleware1, ..., middlewareN, 'controller.action');
router.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller.action');
router.verb('path-match', app.controller.action);
router.verb('router-name', 'path-match', app.controller.action);
router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);
```
The complete definition of router includes 5 major parts:

Expand All @@ -57,15 +60,6 @@ The complete definition of router includes 5 major parts:
* `app.controller.user.fetch` - directly point to a controller
* `'user.fetch'` - simplified as a string,

### `app.verb` support

To make define routing rules more easier, we also support `abb.verb` like `app.get`, `app.post` to define a routing rule. But because HTTP methods are to much, and some methods are overrided(e.x. `app.options` is overrided).

```js
app.get('/home', app.controller.home);
// EQUAL TO app.router.get('/home', app.controller.home);
```

### Notices

- multiple Middlewares can be configured to execute serially in Router definition
Expand All @@ -76,11 +70,15 @@ app.get('/home', app.controller.home);
Here are some examples of writing routing rules:

```js
app.router.get('/home', app.controller.home);
app.router.get('/user/:id', app.controller.user.page);
app.router.post('/admin', isAdmin, app.controller.admin);
app.router.post('user', '/user', isLoginUser, hasAdminPermission, app.controller.user.create);
app.router.post('/api/v1/comments', app.controller.v1.comments.create); // app/controller/v1/comments.js
// app/router.js
module.exports = app => {
const { router, controller } = app;
router.get('/home', controller.home);
router.get('/user/:id', controller.user.page);
router.post('/admin', isAdmin, controller.admin);
router.post('/user', isLoginUser, hasAdminPermission, controller.user.create);
router.post('/api/v1/comments', controller.v1.comments.create); // app/controller/v1/comments.js
};
```

### RESTful style URL definition
Expand All @@ -90,8 +88,9 @@ We provide `app.resources('router-name', 'path-match', 'controller-name')` to ge
```js
// app/router.js
module.exports = app => {
app.router.resources('posts', '/posts', app.controller.posts);
app.router.resources('users', '/api/v1/users', app.controller.v1.users); // app/controller/v1/users.js
const { router, controller } = app;
router.resources('posts', '/posts', controller.posts);
router.resources('users', '/api/v1/users', controller.v1.users); // app/controller/v1/users.js
};
```

Expand Down
52 changes: 23 additions & 29 deletions docs/source/zh-cn/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ Router 主要用来描述请求 URL 和具体承担执行动作的 Controller
```js
// app/router.js
module.exports = app => {
app.router.get('/user/:id', app.controller.user.info);
const { router, controller } = app;
router.get('/user/:id', controller.user.info);
};
```

- `app/controller` 目录下面实现 Controller

```js
// app/controller/user.js
exports.info = async ctx => {
ctx.body = {
name: `hello ${ctx.params.id}`,
};
};
class UserController extends Controller {
async info() {
this.ctx.body = {
name: `hello ${ctx.params.id}`,
};
}
}
```

这样就完成了一个最简单的 Router 定义,当用户执行 `GET /user/123``user.js` 这个里面的 info 方法就会执行。
Expand All @@ -35,10 +38,10 @@ exports.info = async ctx => {
下面是路由的完整定义,参数可以根据场景的不同,自由选择:

```js
router.verb('path-match', app.controller.controller.action);
router.verb('router-name', 'path-match', app.controller.controller.action);
router.verb('path-match', middleware1, ..., middlewareN, app.controller.controller.action);
router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.controller.action);
router.verb('path-match', app.controller.action);
router.verb('router-name', 'path-match', app.controller.action);
router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);
```

路由完整定义主要包括5个主要部分:
Expand All @@ -60,15 +63,6 @@ router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.cont
* `app.controller.user.fetch` - 直接指定一个具体的 controller
* `'user.fetch'` - 可以简写为字符串形式

### `app.verb` 支持

为了编写路由更简洁,我们也将 router 方法挂载到了 `app` 上,由于 HTTP 方法过多,所以一些方法容易被其他插件覆盖(例如 `app.options` 已被覆盖,不是设置路由的方法),使用时需要注意。

```js
app.get('/home', app.controller.home);
// 等价于 app.router.get('/home', app.controller.home);
```

### 注意事项

- 在 Router 定义中, 可以支持多个 Middleware 串联执行
Expand All @@ -79,14 +73,14 @@ app.get('/home', app.controller.home);
下面是一些路由定义的方式:

```js
// app/rotuer.js
// app/router.js
module.exports = app => {
const { router } = app;
router.get('/home', app.controller.home);
router.get('/user/:id', app.controller.user.page);
router.post('/admin', isAdmin, app.controller.admin);
router.post('/user', isLoginUser, hasAdminPermission, app.controller.user.create);
router.post('/api/v1/comments', app.controller.v1.comments.create); // app/controller/v1/comments.js
const { router, controller } = app;
router.get('/home', controller.home);
router.get('/user/:id', controller.user.page);
router.post('/admin', isAdmin, controller.admin);
router.post('/user', isLoginUser, hasAdminPermission, controller.user.create);
router.post('/api/v1/comments', controller.v1.comments.create); // app/controller/v1/comments.js
};
```

Expand All @@ -98,9 +92,9 @@ module.exports = app => {
```js
// app/router.js
module.exports = app => {
const { router } = app;
router.resources('posts', '/api/posts', app.controller.posts);
router.resources('users', '/api/v1/users', app.controller.v1.users); // app/controller/v1/users.js
const { router, controller } = app;
router.resources('posts', '/api/posts', controller.posts);
router.resources('users', '/api/v1/users', controller.v1.users); // app/controller/v1/users.js
};
```

Expand Down

0 comments on commit bdfd3cc

Please sign in to comment.