Skip to content

Commit 7c62e7e

Browse files
SF-Zhoupopomore
authored andcommitted
feat: add cnode api async example (#22)
1 parent e255fb6 commit 7c62e7e

File tree

17 files changed

+634
-0
lines changed

17 files changed

+634
-0
lines changed

cnode-api-async/.autod.conf.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
module.exports = {
4+
write: true,
5+
prefix: '^',
6+
test: [
7+
'test',
8+
'benchmark',
9+
],
10+
dep: [
11+
],
12+
devdep: [
13+
'egg-ci',
14+
'egg-bin',
15+
'autod',
16+
'eslint',
17+
'eslint-config-egg',
18+
'supertest',
19+
'webstorm-disable-index',
20+
],
21+
exclude: [
22+
'./test/fixtures',
23+
'./dist',
24+
],
25+
};
26+

cnode-api-async/.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/fixtures
2+
coverage

cnode-api-async/.eslintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "eslint-config-egg",
3+
"parserOptions": {
4+
"ecmaVersion": 2017
5+
},
6+
"rules": {
7+
},
8+
"globals": {
9+
"app": true,
10+
"request": true,
11+
"mm": true,
12+
"mock": true,
13+
"assert": true
14+
}
15+
}

cnode-api-async/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
logs/
2+
npm-debug.log
3+
node_modules/
4+
coverage/
5+
.idea/
6+
run/
7+
.DS_Store
8+
*.swp
9+

cnode-api-async/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# cnode-api-async
2+
3+
4+
5+
## QuickStart
6+
7+
<!-- add docs here for user -->
8+
9+
see [egg docs][egg] for more detail.
10+
11+
### Development
12+
```shell
13+
$ npm install
14+
$ npm run dev
15+
$ open http://localhost:7001/news
16+
```
17+
18+
### Deploy
19+
20+
Use `EGG_SERVER_ENV=prod` to enable prod mode
21+
22+
```shell
23+
$ EGG_SERVER_ENV=prod npm start
24+
```
25+
26+
### npm scripts
27+
28+
- Use `npm run lint` to check code style.
29+
- Use `npm test` to run unit test.
30+
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
31+
32+
33+
[egg]: https://eggjs.org

cnode-api-async/README.zh-CN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# cnode-api-async
2+
3+
4+
5+
## 快速入门
6+
7+
<!-- 在此次添加使用文档 -->
8+
9+
如需进一步了解,参见 [egg 文档][egg]
10+
11+
### 本地开发
12+
```bash
13+
$ npm install
14+
$ npm run dev
15+
$ open http://localhost:7001/news
16+
```
17+
18+
### 部署
19+
20+
线上正式环境用 `EGG_SERVER_ENV=prod` 来启动。
21+
22+
```bash
23+
$ EGG_SERVER_ENV=prod npm start
24+
```
25+
26+
### 单元测试
27+
- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。
28+
- 断言库非常推荐使用 [power-assert]
29+
- 具体参见 [egg 文档 -单元测试](https://eggjs.org/zh-cn/core/unittest)
30+
31+
### 内置指令
32+
33+
- 使用 `npm run lint` 来做代码风格检查。
34+
- 使用 `npm test` 来执行单元测试。
35+
- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod)
36+
37+
38+
[egg]: https://eggjs.org

cnode-api-async/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const co = require('co');
4+
5+
module.exports = app => {
6+
app.curl = co.wrap(app.curl);
7+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
class TopicsController extends app.Controller {
5+
constructor(ctx) {
6+
super(ctx);
7+
8+
this.createRule = {
9+
accesstoken: 'string',
10+
title: 'string',
11+
tab: { type: 'enum', values: [ 'ask', 'share', 'job' ], required: false },
12+
content: 'string',
13+
};
14+
}
15+
16+
async show() {
17+
const { ctx } = this;
18+
19+
ctx.body = await ctx.service.topics.show({
20+
id: ctx.params.id,
21+
mdrender: ctx.query.mdrender !== 'false',
22+
accesstoken: ctx.query.accesstoken || '',
23+
});
24+
}
25+
26+
async index() {
27+
const { ctx } = this;
28+
29+
ctx.validate({
30+
page: { format: /\d+/, required: false },
31+
tab: { type: 'enum', values: [ 'ask', 'share', 'job', 'good' ], required: false },
32+
limit: { format: /\d+/, required: false },
33+
}, ctx.query);
34+
35+
ctx.body = await ctx.service.topics.list({
36+
page: ctx.query.page,
37+
tab: ctx.query.tab,
38+
limit: ctx.query.limit,
39+
mdrender: ctx.query.mdrender !== 'false',
40+
});
41+
}
42+
43+
async create() {
44+
const { ctx } = this;
45+
ctx.validate(this.createRule);
46+
47+
const id = await ctx.service.topics.create(ctx.request.body);
48+
ctx.body = {
49+
topic_id: id,
50+
};
51+
ctx.status = 201;
52+
}
53+
54+
async update() {
55+
const { ctx } = this;
56+
57+
ctx.validate(this.createRule);
58+
await ctx.service.topics.update(ctx.params.id, ctx.request.body);
59+
ctx.status = 204;
60+
}
61+
}
62+
63+
return TopicsController;
64+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
module.exports = (option, app) => {
4+
return async function(ctx, next) {
5+
try {
6+
await next();
7+
} catch (err) {
8+
// 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志
9+
app.emit('error', err, this);
10+
const status = err.status || 500;
11+
// 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
12+
const error = status === 500 && app.config.env === 'prod'
13+
? 'Internal Server Error'
14+
: err.message;
15+
// 从 error 对象上读出各个属性,设置到响应中
16+
ctx.body = { error };
17+
if (status === 422) {
18+
ctx.body.detail = err.errors;
19+
}
20+
ctx.status = status;
21+
}
22+
};
23+
};

cnode-api-async/app/router.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.resources('topics', '/api/v2/topics', 'topics');
5+
};

0 commit comments

Comments
 (0)