server.js
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
49
50
51
52
53
54
55
56
const express = require('express')
const bodyParser = require('body-parser')
const route = require('./server/route')
const cors = require('cors')
const timeout = require('connect-timeout')
const _ = require('lodash')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(timeout('5s'))
app.use(function (req, res, next) {
res.error = function error(data) {
res.json({
code: 400,
message: data || 'params wrong'
})
}
res.success = function (data) {
res.json({
code: 200,
message: 'success',
data: data
})
}
req.body = req.method.toUpperCase() === 'GET' ? req.query : req.body
next()
})
app.use('/api/v1/yoho', cors(), route)
app.all('*', function (req, res, next) {
res.json({
code: 500,
message: 'server wrong'
})
})
app.use(function (err, req, res, next) {
console.error(err)
res.json({
code: 500,
message: err
})
})
module.exports = app