-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
74 lines (60 loc) · 1.69 KB
/
server.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as bodyParser from 'body-parser'
import * as compression from 'compression'
import * as cookieParser from 'cookie-parser'
import * as express from 'express'
import * as helmet from 'helmet'
import * as mongoose from 'mongoose'
import * as logger from 'morgan'
import * as cors from 'cors'
import { route } from './routes'
import * as path from 'path'
class Server {
private express: express.Express
constructor() {
this.express = express()
this.setupExpress()
}
public start(port: number, callback: () => void): void {
this.express.listen(port, callback)
this.connectMongoose()
}
/**
* Setup Express server
*/
private setupExpress(): void {
this.middleware()
this.routes()
}
/**
* Setup Express server
*/
private connectMongoose(): void {
const MONGO_URI = 'mongodb://localhost/ment'
mongoose.connect(MONGO_URI)
mongoose.connection.on('connected', () => {
console.log(`Connected to database ${MONGO_URI}`)
})
mongoose.connection.on('error', (e) => {
console.log(`Error connecting to database ${MONGO_URI}`)
console.log(e)
})
}
/**
* Setup Express middlewares
*/
private middleware(): void {
this.express.set('twig options', { strict_variables: false })
this.express.use(express.static(path.join(__dirname, '../public')))
this.express.use(bodyParser.urlencoded({ extended: true }))
this.express.use(bodyParser.json())
this.express.use(cors())
this.express.use(cookieParser())
this.express.use(logger('dev'))
this.express.use(compression())
this.express.use(helmet())
}
private routes(): void {
this.express.use(route)
}
}
export let server = new Server()