-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
70 lines (58 loc) · 1.83 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
59
60
61
62
63
64
65
66
67
68
69
70
'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')
const helmet = require('helmet')
const flash = require('connect-flash')
const fileUpload = require('express-fileupload')
// body parse
const app = express()
// helmet
app.use(helmet())
app.use(helmet.hidePoweredBy())
// cors
app.use(cors({
origin: config.get('cors')
}))
app.use(morgan('short', { stream: logger.stream }))
const server = require('http').Server(app)
app.use(flash())
app.use(fileUpload())
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'))
app.use(require('./middlewares/sitemap'))
// error handler
app.use(require('./middlewares/error'))
app.get('*', function (req, res) {
let p
if (process.env.NODE_ENV === 'development') {
p = path.resolve(__dirname, 'index.html')
} else {
p = path.resolve(__dirname, './build', 'index.html')
}
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