-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (64 loc) · 1.7 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
// import https from 'https'
// import fs from 'fs'
import express from 'express'
import session from 'express-session'
import helmet from 'helmet'
import bodyParser from 'body-parser'
import expressValidator from 'express-validator'
import compression from 'compression'
import debug from './src/lib/debug'
import routes from './routes'
import * as validation from './src/api/validation'
debug.log('Server initializing ...')
let app = express()
// Security
app.use(helmet())
// Compression
app.use(compression())
// Body parser and Validator
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(expressValidator({
customValidators: {
isObjectId: validation.isObjectId,
gameIsExist: validation.gameIsExist
}
}))
// Views
app.set('view engine', 'pug')
app.set('views', './views')
// Session
app.use(session({
secret: 'ieem-game',
resave: false,
saveUninitialized: false
}))
// Setting
app.set('port', process.env.PORT || 80)
app.set('title', '2017 工工營 產銷遊戲')
// Static
app.use('/', express.static('public'))
app.use('/', express.static('views'))
if (process.env.NODE_ENV !== 'production') {
app.use('/test', express.static('test'))
app.use(function (req, res, next) {
//debug.log(req.connection.remoteAddress, req.method, req.path)
next()
})
}
// Route
routes(app)
// Listening
/*
const options = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
}
https.createServer(options, app).listen(app.get('port'), function () {
debug.log('Stating listening ...')
})
*/
app.listen(app.get('port'), function () {
debug.log('Start to listen on PORT %d ...', app.get('port'))
})
debug.log('Server initialized done')