Skip to content

Commit 14b7e29

Browse files
egg-update
1 parent 527ea42 commit 14b7e29

19 files changed

+253
-63
lines changed

17-nodejs/02-egg/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ coverage/
66
run/
77
logs/
88
.DS_Store
9-
.vscode
109
*.swp
1110
*.lock
1211
*.js
@@ -18,3 +17,5 @@ config/**/*.js
1817
app/**/*.map
1918
test/**/*.map
2019
config/**/*.map
20+
21+
!database/**/*

17-nodejs/02-egg/.vscode/launch.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch Egg",
6+
"type": "node",
7+
"request": "launch",
8+
"cwd": "${workspaceRoot}",
9+
"runtimeExecutable": "npm",
10+
"windows": { "runtimeExecutable": "npm.cmd" },
11+
"runtimeArgs": [ "run", "debug" ],
12+
"console": "integratedTerminal",
13+
"protocol": "auto",
14+
"restart": true,
15+
"port": 9229,
16+
"autoAttachChildProcesses": true
17+
}
18+
]
19+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"eslint.validate": [
3+
"javascript",
4+
"javascriptreact",
5+
{ "language": "typescript", "autoFix": true },
6+
{ "language": "typescriptreact", "autoFix": true },
7+
]
8+
}

17-nodejs/02-egg/LICENSE

+22-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
MIT LICENSE
1+
MIT LICENSE
2+
3+
Copyright (c) 2015-present Ant UED, https://xtech.antfin.com/
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

17-nodejs/02-egg/README.md

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# hackernews-async-ts
2-
3-
[Hacker News](https://news.ycombinator.com/) showcase using typescript && egg
1+
# egg-demo-csxiaoyao
42

53
## QuickStart
64

@@ -9,7 +7,7 @@
97
```bash
108
$ npm i
119
$ npm run dev
12-
$ open http://localhost:7001/
10+
$ open http://localhost:2048/
1311
```
1412

1513
Don't tsc compile at development mode, if you had run `tsc` then you need to `npm run clean` before `npm run dev`.
@@ -32,26 +30,31 @@ $ npm start
3230
- Node.js 8.x
3331
- Typescript 2.8+
3432

35-
33+
## Other
34+
Need to start `mysql` & `redis`
35+
```
36+
$ brew services start mysql
3637
$ redis-server
38+
```
39+
40+
## TODO
41+
1. docs
42+
2. whistle & domain
43+
3. login & passport & auth
44+
4. husky
45+
5. lint-staged
46+
6. prettier
47+
48+
## Docs
49+
**validate**
50+
51+
https://github.com/node-modules/parameter#rule
52+
53+
**mocha**
54+
55+
https://mochajs.cn/
56+
57+
**sequelize**
3758

38-
redis
39-
seed
40-
docs
41-
cros
42-
TDD
43-
域名
44-
登录态
45-
权限校验
46-
whistle
47-
husky
48-
lint-staged
49-
prettier
50-
51-
52-
sequelize
5359
https://sequelize.org/master/manual/query-interface.html
5460
https://sequelize.org/master/variable/index.html#static-variable-DataTypes
55-
56-
validate
57-
https://github.com/node-modules/parameter#rule

17-nodejs/02-egg/app/middleware/errorHandler.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* 错误处理中间件
3+
*/
14
export default () => {
25
return async function errorHandler(ctx, next) {
36
try {

17-nodejs/02-egg/app/middleware/robot.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
2-
1+
/**
2+
* robot拦截
3+
*/
34
// options === app.config.robot
45
export default (options, app) => {
56
console.log(app.config.robot);

17-nodejs/02-egg/app/model/user.ts

-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ export default function(app: Application) {
3333
app.model.User.hasMany(app.model.Post, { as: 'posts' });
3434
}
3535
};
36-
3736
// return User;
3837
}

17-nodejs/02-egg/app/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default (app: Application) => {
77
router.get('/api/demo', controller.demo.testEnv);
88
router.get('/api/demo/:id', controller.demo.testThrowError);
99
router.post('/api/demo', controller.demo.testRedis);
10-
10+
// restful
1111
router.resources('users', '/api/users', controller.user);
1212
router.resources('posts', '/api/posts', controller.post);
1313
};

17-nodejs/02-egg/config/Code.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
SUCCESS: '0',
3+
TEST_ERROR: '-1', // only for test
4+
CURL_ERROR: '-1000',
5+
DATA_NOT_FOUND: '404',
6+
};

17-nodejs/02-egg/config/config.default.ts

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
/**
2+
* config default
3+
* csxiaoyao
4+
* 2020.07.19
5+
*/
16
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
7+
import CODE from './CODE';
28

39
export default (appInfo: EggAppInfo) => {
410
const config = {} as PowerPartial<EggAppConfig>;
@@ -7,13 +13,7 @@ export default (appInfo: EggAppInfo) => {
713
// use for cookie sign key, should change to your own and keep security
814
config.keys = appInfo.name + '_csxiaoyao';
915

10-
config.CODE = {
11-
SUCCESS: '0',
12-
TEST_ERROR: '-1',
13-
CURL_ERROR: '-1000',
14-
DATA_NOT_FOUND: '404',
15-
};
16-
16+
// mysql
1717
config.sequelize = {
1818
dialect: 'mysql',
1919
host: '127.0.0.1',
@@ -24,55 +24,58 @@ export default (appInfo: EggAppInfo) => {
2424
timezone: '+08:00',
2525
};
2626

27+
// redis
28+
config.redis = {
29+
client: {
30+
port: 6379,
31+
host: '127.0.0.1',
32+
password: '',
33+
db: 0,
34+
},
35+
};
36+
37+
// 全局错误处理
2738
config.onerror = {
2839
all(err, ctx) {
2940
console.log(err);
30-
// 在此处定义针对所有响应类型的错误处理方法
31-
// 注意,定义了 config.all 之后,其他错误处理方法不会再生效
3241
ctx.body = 'error';
3342
ctx.status = 500;
3443
},
3544
html(err, ctx) {
3645
console.log(err);
37-
// html hander
3846
ctx.body = '<h3>error</h3>';
3947
ctx.status = 500;
4048
},
4149
json(err, ctx) {
4250
console.log(err);
43-
// json hander
4451
ctx.body = { message: 'error' };
4552
ctx.status = 500;
4653
},
4754
};
4855

56+
// body数据解析
4957
const bodyParser = {
5058
jsonLimit: '3mb',
5159
// ignore: '/api',
5260
};
5361

54-
// add your egg config in here
62+
/**
63+
* 自定义中间件配置
64+
*/
5565
config.middleware = [
5666
'robot',
5767
'errorHandler',
5868
];
59-
60-
// robot's configurations
6169
const robot = {
62-
ua: [
63-
/curl/i,
64-
/Baiduspider/i,
65-
],
70+
ua: [ /curl/i ],
6671
};
67-
68-
// 只对 /api 前缀的 url 路径生效
6972
const errorHandler = {
70-
match: '/api',
73+
match: '/api', // 只对 /api 前缀的 url 路径生效
7174
};
7275

73-
// the return config will combines to EggAppConfig
7476
return {
7577
...config,
78+
CODE,
7679
bodyParser,
7780
robot,
7881
errorHandler,

17-nodejs/02-egg/config/config.local.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EggAppConfig, PowerPartial } from 'egg';
33
export default () => {
44
const config: PowerPartial<EggAppConfig> = {};
55

6+
// 日志输出等级调整
67
config.logger = {
78
level: 'DEBUG', // 输出到文件级别
89
consoleLevel: 'DEBUG', // 输出到console级别
@@ -14,15 +15,6 @@ export default () => {
1415
},
1516
};
1617

17-
config.redis = {
18-
client: {
19-
port: 6379,
20-
host: '127.0.0.1',
21-
password: '',
22-
db: 0,
23-
},
24-
};
25-
2618
return {
2719
...config,
2820
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
module.exports = {
4+
// 在执行数据库升级时调用的函数,创建 users 表
5+
up: async (queryInterface, Sequelize) => {
6+
/**
7+
* Add altering commands here.
8+
*
9+
* Example:
10+
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
11+
*/
12+
const { BIGINT, INTEGER, DATE, STRING, NOW } = Sequelize;
13+
await queryInterface.createTable('users', {
14+
id: {
15+
type: BIGINT(11),
16+
primaryKey: true,
17+
allowNull: false,
18+
unique: true,
19+
autoIncrement: true,
20+
},
21+
name: STRING(30),
22+
age: INTEGER,
23+
created_at: {
24+
type: DATE,
25+
defaultValue: NOW,
26+
allowNull: false
27+
},
28+
updated_at: {
29+
type: DATE,
30+
defaultValue: NOW,
31+
allowNull: false
32+
},
33+
});
34+
},
35+
// 在执行数据库降级时调用的函数,删除 users 表
36+
down: async (queryInterface, Sequelize) => {
37+
/**
38+
* Add reverting commands here.
39+
*
40+
* Example:
41+
* await queryInterface.dropTable('users');
42+
*/
43+
await queryInterface.dropTable('users');
44+
},
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
module.exports = {
4+
up: async (queryInterface, Sequelize) => {
5+
const { INTEGER, STRING, DATE, NOW } = Sequelize;
6+
await queryInterface.createTable('posts', {
7+
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
8+
title: STRING(30),
9+
content: STRING(255),
10+
user_id: INTEGER,
11+
created_at: {
12+
type: DATE,
13+
defaultValue: NOW,
14+
allowNull: false
15+
},
16+
updated_at: {
17+
type: DATE,
18+
defaultValue: NOW,
19+
allowNull: false
20+
},
21+
});
22+
},
23+
24+
down: async queryInterface => {
25+
await queryInterface.dropTable('posts');
26+
},
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports = {
4+
up: async (queryInterface, Sequelize) => {
5+
const { STRING } = Sequelize;
6+
await queryInterface.addColumn('users', 'gender', STRING(10), {
7+
after: 'age'
8+
});
9+
},
10+
11+
down: async (queryInterface, Sequelize) => {
12+
await queryInterface.removeColumn('users', 'gender');
13+
}
14+
};

0 commit comments

Comments
 (0)