-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (41 loc) · 1 KB
/
Copy pathapp.js
File metadata and controls
48 lines (41 loc) · 1 KB
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
const express = require('express')
const userAgent = require('express-useragent')
const app = express()
// 输出环境信息
console.log('运行环境:', process.env.NODE_ENV)
console.log('程序版本:', process.env.APP_VERSION)
// 去掉header的服务器版本
app.set('x-powered-by', false)
// userAgent
app.use(userAgent.express())
// 解析jsonBody,解析form表单
app.use(express.json({ limit: '2mb' }))
app.use(express.urlencoded({
limit: '2mb',
extended: false
}))
// 解析IP
app.use(async function (req, res, next) {
// UTIL.Allow_CORS_ALL(res)
if (req.method === 'OPTIONS') {
res.status(202)
res.end()
return
}
next()
})
// 业务路由
app.use('/api', require('./routes'))
app.use(function (req, res) {
res.status(404).send({
error: true,
msg: 'Invalid API Route'
})
})
app.use(function (err, req, res) {
res.status(err.status || 500).send({
error: true,
msg: 'Application Error'
})
})
module.exports = app