-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
58 lines (47 loc) · 1.49 KB
/
index.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
'use strict'
const express = require('express')
const config = require('config')
const bodyParser = require('body-parser')
const validator = require('express-validator')
const path = require('path')
const yaml = require('js-yaml')
const fs = require('fs')
const cors = require('cors')
const swaggerUi = require('swagger-ui-express')
const morgan = require('morgan')
const logger = require('./helpers/logger')
// body parse
const app = express()
// cors
app.use(cors({
origin: config.get('cors')
}))
app.use(morgan('short', { stream: logger.stream }))
const server = require('http').Server(app)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(validator({}))
app.use('/build', express.static('build'))
app.use('/app/assets', express.static('app/assets'))
const docs = yaml.safeLoad(fs.readFileSync('./docs/swagger.yml', 'utf8'))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(docs))
// apis
app.use(require('./apis'))
let p
if (process.env.NODE_ENV === 'development') {
p = path.resolve(__dirname, 'index.html')
} else {
p = path.resolve(__dirname, './build', 'index.html')
}
app.get('*', function (req, res) {
return res.sendFile(p)
})
// error handler
app.use(require('./middlewares/error'))
// start server
server.listen(config.get('server.port'), config.get('server.host'), function () {
const host = server.address().address
const port = server.address().port
console.info('Server start at http://%s:%s', host, port)
})
module.exports = app