-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (33 loc) · 1005 Bytes
/
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
//导入express模块
const express = require('express')
// 导入 cors 中间件
const cors = require('cors')
//引入路由
const buck_data_router = require('./router/inforouter')
// 导入@hapi/joi
const joi = require('joi')
//创建express的服务器实例
const app = express()
app.use(express.static('./static'))
// 重新封装的res.send()
app.use(function (req, res, next) {
// status=0为成功 status=1为失败,默认为1
res.cc = function (err, status = 1) {
res.send({
// 状态
status,
// 状态描述,判断err是错误对象还是字符串
message: err instanceof Error ? err.message : err,
})
}
next()
})
// cors跨越
app.use(cors())
// ,配置解析 application/x-www-form-urlencoded 格式的表单数据的中间件
app.use(express.urlencoded({ extended: false }))
// 注册登录路由模块
app.use('/api', buck_data_router)
app.listen(8080,()=>{
console.log('api serve running')
})