-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
67 lines (59 loc) · 3.03 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
const express = require('express')
const path = require('path')
const session = require('express-session')
//-- Express --------------------------------------------------------------------------------------------------
if (process.env.NODE_ENV !== 'production')
require('dotenv').config() // devDependency to setup .env locally
const app = express()
const PORT = process.env.PORT
const server = require('http').Server(app)
const io = require('socket.io')(server)
//-------------------------------------------------------------------------------------------------------------
//-- Body parser ----------------------------------------------------------------------------------------------
const bodyParser = require("body-parser");
app.use(bodyParser.json());
//-------------------------------------------------------------------------------------------------------------
//-- Set Session Cookies ---------------------------------------------------------------------------------------
app.use(session({
name: 'sessionID',
secret: 'winterrunfarmhulkyolkmarinaetymologycors',
cookie: { maxAge: 1000 * 60 * 60 * 24 * 30 },
resave: true,
saveUninitialized: false
}))
//-------------------------------------------------------------------------------------------------------------
//-- Logger ---------------------------------------------------------------------------------------------------
const morgan = require('morgan')
const cors = require('cors')
if (process.env.NODE_ENV !== 'production') {
app.use(cors({ credentials: true, origin: 'http://localhost:3000' }));
app.use(morgan('dev'))
}
//-------------------------------------------------------------------------------------------------------------
//-- Socket ---------------------------------------------------------------------------------------------------
io.on('connection', socket => {
socket.on('join-bidding', data => {
socket.join(data.resource)
})
socket.on('leave-bidding', data => {
socket.leave(data.resource)
})
})
//-------------------------------------------------------------------------------------------------------------
//-- Code start here ------------------------------------------------------------------------------------------
app.use('/api', (req, res, next) => {
req.io = io
next()
}, require('./api'))
//-------------------------------------------------------------------------------------------------------------
//-- Heroku ---------------------------------------------------------------------------------------------------
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function (_req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
//-------------------------------------------------------------------------------------------------------------
server.listen(PORT, () => console.log(`Knowledge Bytes running at ${PORT}`))