-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
71 lines (60 loc) · 2.21 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
const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const keys = require('./config/keys');
const socketIO = require('socket.io');
const http = require('http');
require('./models');
require('./config/passport');
mongoose.connect(keys.mongoURI);
const app = express();
// app.use(
// cookieSession({
// maxAge: 30 * 24 * 60 * 60 * 1000,
// keys: [keys.cookieKey]
// })
// );
app.use(session({secret: keys.cookieKey, resave: true, saveUninitialized:true}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
const server = http.createServer(app);
const io = socketIO(server);
io.on('connection', socket => {
console.log('New client connected')
// just like on the client side, we have a socket.on method that takes a callback function
socket.on('change color', (color) => {
// once we get a 'change color' event from one of our clients, we will send it to the rest of the clients
// we make use of the socket.emit method again with the argument given to use from the callback function above
console.log('Color Changed to: ', color)
io.sockets.emit('change color', color)
})
socket.on('update messages', (data, callback) =>{
console.log('Updating Messages');
io.sockets.emit('update messages');
})
// disconnect is fired when a client leaves the server
socket.on('disconnect', () => {
console.log('user disconnected')
})
})
require('./routes/apiRoutes')(app);
require('./routes/authRoutes')(app);
require('./routes/mobileRoutes')(app);
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
// like our main.js file, or main.css file!
app.use(express.static('client/build'));
// Express will serve up the index.html file
// if it doesn't recognize the route
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 3001;
server.listen(PORT);