-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.ts
62 lines (48 loc) · 1.79 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express, { ErrorRequestHandler, RequestHandler, Router } from 'express';
import bodyParser from 'express';
import Err from './src/enums/code';
import { entry, resJSON, entryLog } from './src/utils/helper';
const app = express();
const businessErrorHandler: ErrorRequestHandler = (err, req, res, next) => {
if (err.code) {
if (process.env.FULL_LOG !== 'off' || err.code !== Err.CODE.PARAMS_INVALID) req.logCtx.errStack = err.stack;
if (err.original) req.logCtx.original = err.original;
return res.json(resJSON(err.code, err.message, null, req.logCtx));
}
if (err.isJoi) {
res.status(200);
req.logCtx.msgIn = err.message;
return res.json(resJSON(Err.CODE.PARAMS_INVALID, err.message, null, req.logCtx));
}
return next(err);
};
const notFoundHandler: RequestHandler = (req, res) => {
res.status(404);
const logCtx = req.logCtx;
logCtx.brief = 'IN uri error: ' + req.originalUrl;
logCtx.logLevel = 'error';
logCtx.msgIn = 'Not Found';
return res.json(resJSON(Err.CODE.NOT_FOUND, 'Not Found', null, req.logCtx));
};
const internalErrorHandler: ErrorRequestHandler = (err, req, res, next) => {
res.status(500);
const logCtx = req.logCtx;
logCtx.brief = 'IN uri error: ' + req.originalUrl;
logCtx.logLevel = 'error';
logCtx.errStack = err.stack || err;
return res.json(resJSON(Err.CODE.UNKNOWN, '内部错误', null, logCtx));
};
export default function getApp(router: Router) {
app.use('*', entry);
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.raw({ limit: '10mb' }));
if (process.env.FULL_LOG !== 'off') {
app.all('*', entryLog);
}
app.use(router);
app.use(businessErrorHandler);
app.use(notFoundHandler);
app.use(internalErrorHandler);
return app;
}