server.js
885 Bytes
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
const express = require('express')
const bodyParser = require('body-parser')
const route = require('./server/route')
const cors = require('cors')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(function(req, res, next) {
res.error = function error(data) {
res.json(Object.assign({
code: 400,
message: 'params wrong'
}, data))
}
res.success = function(data) {
res.json(Object.assign({
code: 200,
message: 'successful'
}, data))
}
req.body = req.query ? req.query: req.body
})
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) {
res.json({
code: 500,
message: err
})
})
module.exports = app