Skip to content

Commit a543c54

Browse files
SunShinewyfatian25
authored andcommitted
docs(hackernews): upgrade to egg2.x (#48)
1 parent f0ffc18 commit a543c54

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

hackernews/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# egg-example-hackernews-async
1+
## About this example
2+
This example is a [quickstart](https://eggjs.org/zh-cn/intro/quickstart.html) of Egg, it includes the basic and comprehensive usages of Egg such as Controller, Service and some
3+
simple configurations, moreover, it has the corresponding unit tests. you can follow this simple example and build your Egg application step by step.
24

3-
[Hacker News](https://news.ycombinator.com/) showcase using async/await for egg
4-
5-
## QuickStart
5+
## How to run it locally
66

77
### Development
88
```shell
@@ -27,11 +27,10 @@ $ EGG_SERVER_ENV=prod npm start
2727

2828
### Requirement
2929

30-
Please ensure your node version is `>=7.6.0` for async await support without flag. If your node version is `>=7.0.0 < 7.6.0`, you can run npm scripts with harmony flag
30+
Please ensure your node version is `>=8.0` for Egg2.x.
3131

32-
```shell
33-
# start server
34-
npm run dev -- --harmony-async-await
35-
# run test cases
36-
npm run test-local -- --harmony-async-await
37-
```
32+
33+
## some problems you might encounter
34+
- lack of MVC model development experience, click [here](https://www.zhihu.com/question/27897315) to learn more
35+
- some concepts such as middleware, extend, helper are difficult to understand, see related [docs](https://eggjs.org/) to know more
36+
- encounter some errors during development, find [issues](https://github.com/eggjs/egg/issues) to check if some solutions have been provided

hackernews/app/router.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
module.exports = app => {
4+
const { router, controller } = app;
45
app.redirect('/', '/news');
5-
app.router.get('/news', 'news.list');
6-
app.router.get('/news/item/:id', 'news.detail');
7-
app.router.get('/news/user/:id', 'news.user');
6+
router.get('/news', controller.news.list);
7+
router.get('/news/item/:id', controller.news.detail);
8+
router.get('/news/user/:id', controller.news.user);
89
};

hackernews/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
"description": "hackernews showcase using async/await for egg",
55
"private": true,
66
"dependencies": {
7-
"egg": "^1.10.1",
7+
"egg": "^2.0.0",
88
"egg-view-nunjucks": "^2.1.4",
9-
"moment": "^2.19.1"
9+
"moment": "^2.19.2"
1010
},
1111
"devDependencies": {
12+
"autod": "^3.0.1",
13+
"autod-egg": "^1.0.0",
1214
"cheerio": "^1.0.0-rc.2",
1315
"egg-bin": "^4.3.5",
14-
"egg-mock": "^3.13.1"
16+
"egg-mock": "^3.13.1",
17+
"eslint": "^4.12.0",
18+
"eslint-config-egg": "^5.1.1"
1519
},
1620
"engines": {
1721
"node": ">=8.9.0"

hackernews/test/app/controller/news.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
'use strict';
22

33

4-
const mm = require('egg-mock');
5-
const assert = require('assert');
64
const cheerio = require('cheerio');
5+
const { mock, assert } = require('egg-mock/bootstrap');
76

87
describe('test/app/controller/news.test.js', () => {
98
let app;
109
before(async () => {
11-
app = mm.app();
10+
app = mock.app();
1211
await app.ready();
1312
});
1413

1514
after(() => app.close());
1615

17-
afterEach(mm.restore);
16+
afterEach(mock.restore);
1817

1918
it('should GET /news', async () => {
2019
const result = await app.httpRequest().get('/news').expect(200);

hackernews/test/app/service/HackerNews.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
'use strict';
22

3-
const assert = require('assert');
4-
const mm = require('egg-mock');
3+
const { mock, assert } = require('egg-mock/bootstrap');
54

65
describe('test/app/service/HackerNews.test.js', () => {
76
let app;
87
let ctx;
98

109
before(async () => {
11-
app = mm.app();
10+
app = mock.app();
1211
await app.ready();
1312
ctx = app.mockContext();
1413
});
1514

1615
after(() => app.close());
17-
afterEach(mm.restore);
16+
afterEach(mock.restore);
1817

1918
it('getTopStories', async () => {
2019
const list = await ctx.service.hackerNews.getTopStories();

0 commit comments

Comments
 (0)