forked from wassgha/digital-signage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (81 loc) · 2.45 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* eslint-disable multiline-comment-style */
const express = require('express')
const next = require('next')
const mongoose = require('mongoose')
const passport = require('passport')
const cookieParser = require('cookie-parser')
const session = require('cookie-session')
const bodyParser = require('body-parser')
const socketIo = require('socket.io')
const Keys = require('./keys')
const dev = Keys.ENVIRON !== 'PROD'
const app = next({ dev })
const routes = require('./routes')
const handle = routes.getRequestHandler(app)
const apiRoutes = require('./api/routes')
const User = require('./api/models/User')
app
.prepare()
.then(() => {
const server = express()
// Allows for cross origin domain request:
server.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
// MongoDB
mongoose.Promise = Promise
mongoose.connect(
Keys.MONGODB_URI,
{ useNewUrlParser: true }
)
const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
// Parse application/x-www-form-urlencoded
server.use(bodyParser.urlencoded({ extended: false }))
// Parse application/json
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: true }))
// Parse cookies
server.use(cookieParser())
// Sessions
server.use(
session({
secret: Keys.SESSION_SECRET,
resave: true,
saveUninitialized: false
})
)
// Passport
passport.use(User.createStrategy())
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
server.use(passport.initialize())
server.use(passport.session())
let io
server.use(function(req, res, next) {
res.io = io
next()
})
// API routes
server.use('/api/v1', apiRoutes)
// Static routes
server.use('/uploads', express.static('uploads'))
// Next.js routes
server.get('*', (req, res) => {
return handle(req, res)
})
const finalServer = server.listen(Keys.PORT, err => {
if (err) throw err
// eslint-disable-next-line
console.log('> Ready on http://localhost:' + Keys.PORT)
})
// Socket.io
io = socketIo.listen(finalServer)
})
.catch(ex => {
// eslint-disable-next-line
console.error(ex.stack)
process.exit(1)
})