-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·59 lines (49 loc) · 1.64 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
// 实例化服务器
const express = require('express')
const app = express()
const path = require('path')
// cookie
var cookieParser = require('cookie-parser');
app.use(cookieParser());
// 跨域配置
const cors = require('cors')
app.use(cors())
// 导入配置文件
const config = require('./config')
// 中间件全局变量,需在路由前定义
app.use(function (req, res, next) {
res.cc = function (err, status = false) {
res.send({
status,
message: err instanceof Error ? err.message : err,
})
}
next()
})
// Socket
require('./websocket/index');
////////////////////////////////////////////////////////////////////
//解析表单中间件
app.use(express.urlencoded({extended: false}))
app.use('/', require('./routes/index'))
////////////////////////////////////////////////////////////////////
app.use('/apidoc', express.static('apidoc'));
app.use('/public', express.static('public'));
////////////////////////////////////////////////////////////////////
// 错误中间件
const joi = require('joi')
app.use(function (err, req, res, next) {
// 数据验证失败
if (err instanceof joi.ValidationError) return res.cc(err)
// 捕获身份认证失败的错误
if (err.name === 'UnauthorizedError') return res.cc('You have no access to this page!')
// 未知错误
res.cc(err)
})
////////////////////////////////////////////////////////////////////
//require("./cron/cron");
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
app.listen(config.deploy.port, function () {
console.log('api server running at http://127.0.0.1:8181')
})