-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
62 lines (47 loc) · 1.35 KB
/
app.js
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
const express = require("express");
const helmet = require("helmet");
const xss = require("xss-clean");
const compression = require("compression");
const cors = require("cors");
const httpStatus = require("http-status");
const morgan = require("./src/config/morgan");
const { errorConverter, errorHandler } = require("./src/middleware/error.mw");
const ApiError = require("./src/util/api-err");
const routes = require("./src/routes");
const db = require("./src/models");
const app = express();
app.use(morgan.successHandler);
app.use(morgan.errorHandler);
// 设置安全性 HTTP 标头
app.use(helmet());
// 解析 JSON 请求正文
app.use(express.json());
// 解析 urlencode 请求正文
app.use(express.urlencoded({ extended: true }));
// 防xss攻击
app.use(xss());
// gzip压缩
app.use(compression());
// 启用cors
app.use(cors());
app.options('*', cors());
// 启动前,同步数据库
db.sequelize.sync()
.then(() => {
console.log("Synced db.");
})
.catch((err) => {
console.log("Failed to sync db: " + err.message);
});
// 路由入口
app.use('/', routes)
// 为任何未知 API 请求发回 404 错误
app.use((req, res, next) => {
console.log(req);
next(new ApiError(httpStatus.NOT_FOUND, 'Not found'));
});
// 将错误转换为 ApiError
app.use(errorConverter);
// 处理错误
app.use(errorHandler);
module.exports = app;