-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.43 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
const http = require('http')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const mongoose = require('mongoose')
const middleware = require('./utils/middleware')
const platformsRouter = require('./controllers/platforms')
const gamesRouter = require('./controllers/games')
const usersRouter = require('./controllers/users')
const userGamesRouter = require('./controllers/user_games')
const loginRouter = require('./controllers/login')
const defaultRouter = require('./controllers/default')
const app = express()
const config = require('./utils/config')
mongoose
.connect(config.mongoUrl)
.then(() => {
console.log('Connected to database')
})
.catch(error => {
console.log(error)
})
mongoose.Promise = global.Promise
// Middleware
app.use(cors())
app.use(bodyParser.json())
app.use(express.static('build'))
app.use(middleware.logger)
app.use(middleware.tokenExtractor)
// Controllers
app.use('/api/platforms', platformsRouter)
app.use('/api/games', gamesRouter)
app.use('/api/users', usersRouter)
app.use('/api/usergames', userGamesRouter)
app.use('/api/login', loginRouter)
app.use('/', defaultRouter)
// Error middleware
app.use(middleware.error)
const server = http.createServer(app)
server.listen(config.port, () => {
console.log(`Server running of port ${config.port}`)
})
server.on('close', () => {
mongoose.connection.close()
})
module.exports = {
app, server
}