Skip to content

Commit

Permalink
docs(quickstart): use app.router (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and dead-horse committed Nov 23, 2017
1 parent 11c6887 commit 1ec1705
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
25 changes: 8 additions & 17 deletions docs/source/en/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Then edit the router file and add a mapping.
```js
// app/router.js
module.exports = app => {
app.get('/', app.controller.home.index);
const { router, controller } = app;
router.get('/', controller.home.index);
};
```

Expand Down Expand Up @@ -231,8 +232,9 @@ module.exports = NewsController;

// app/router.js
module.exports = app => {
app.get('/', app.controller.home.index);
app.get('/news', app.controller.news.list);
const { router, controller } = app;
router.get('/', controller.home.index);
router.get('/news', controller.news.list);
};
```

Expand Down Expand Up @@ -347,7 +349,6 @@ Suppose that we wanted to prohibit accesses from Baidu crawlers.
Smart developers might quickly guess that we can achieve it by adding a [middleware](../basics/middleware.md)
that checks the User-Agent.


```js
// app/middleware/robot.js
// options === app.config.robot
Expand Down Expand Up @@ -429,26 +430,15 @@ All the test files should place at `{app_root}/test/**/*.test.js`.

```js
// test/app/middleware/robot.test.js
const assert = require('assert');
const mock = require('egg-mock');
const { app, mock, assert } = require('egg-mock/bootstrap');

describe('test/app/middleware/robot.test.js', () => {
let app;
before(() => {
app = mock.app();
return app.ready();
});

afterEach(mock.restore);

it('should block robot', () => {
return app.httpRequest()
.get('/')
.set('User-Agent', "Baiduspider")
.expect(403);
});

// ...
});
```

Expand All @@ -457,7 +447,8 @@ Then add `npm scripts`.
```json
{
"scripts": {
"test": "egg-bin test"
"test": "egg-bin test",
"cov": "egg-bin cov"
}
}
```
Expand Down
26 changes: 8 additions & 18 deletions docs/source/zh-cn/intro/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ module.exports = HomeController;
```js
// app/router.js
module.exports = app => {
app.get('/', app.controller.home.index);
const { router, controller } = app;
router.get('/', controller.home.index);
};
```

Expand Down Expand Up @@ -217,8 +218,9 @@ module.exports = NewsController;

// app/router.js
module.exports = app => {
app.get('/', app.controller.home.index);
app.get('/news', app.controller.news.list);
const { router, controller } = app;
router.get('/', controller.home.index);
router.get('/news', controller.news.list);
};
```

Expand Down Expand Up @@ -401,28 +403,15 @@ module.exports = SomeService;

```js
// test/app/middleware/robot.test.js
const assert = require('assert');
const mock = require('egg-mock');
const { app, mock, assert } = require('egg-mock/bootstrap');

describe('test/app/middleware/robot.test.js', () => {
let app;
before(() => {
// 创建当前应用的 app 实例
app = mock.app();
// 等待 app 启动成功,才能执行测试用例
return app.ready();
});

afterEach(mock.restore);

it('should block robot', () => {
return app.httpRequest()
.get('/')
.set('User-Agent', "Baiduspider")
.expect(403);
});

// ...
});
```

Expand All @@ -431,7 +420,8 @@ describe('test/app/middleware/robot.test.js', () => {
```json
{
"scripts": {
"test": "egg-bin test"
"test": "egg-bin test",
"cov": "egg-bin cov"
}
}
```
Expand Down

0 comments on commit 1ec1705

Please sign in to comment.