-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
55 lines (42 loc) · 1.24 KB
/
app.ts
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
import 'dotenv/config'
import * as http from 'http'
import * as path from 'path'
import express from 'express'
import cookieParser from 'cookie-parser'
import * as bodyParser from 'body-parser'
import { initControllers } from './controllers'
import { staticServing } from './build'
import { GlobalEnv } from './modules/env'
const app = express()
const host = GlobalEnv.host
const port = GlobalEnv.port
if (!host || !port || isNaN(port)) {
console.error('Please specific a host and a port.')
process.exit(1)
}
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.set('port', port)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/', express.static(path.resolve(__dirname, './public')))
staticServing(app)
initControllers(app)
// 404.
app.use((req, res) => {
res.status(404).send('404 Not found')
})
// Other errors.
app.use((req, res) => {
res.status(500).send('500 Internal error')
})
const server = http.createServer(app)
server.listen(port, host)
server.on('error', error => {
console.error(`[Error] Server startup failed: ${error}`)
process.exit(1)
})
server.on('listening', () => {
console.log(`Server is going to start at ${host}:${port}...`)
})