-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (83 loc) · 2.63 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
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
96
97
98
99
100
101
102
103
104
105
106
const express = require('express'),
passport = require('passport'),
path = require('path'),
cookieSession = require('cookie-session'),
mongoose = require('mongoose'),
socket = require('socket.io');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Add the config keys
const keys = require('./config/keys');
// Connect to mongoose
mongoose.connect(keys.mongoURI, { useNewUrlParser: true });
// Set up cookies
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey]
})
);
// Set up passport
app.use(passport.initialize());
app.use(passport.session());
require('./services/passport');
// Import Routes
require('./routes/index')(app);
require('./routes/authentication')(app);
// Set up static files
app.use(express.static(path.join(__dirname, "client", "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
// Send 404 if no other route matched
app.use((req, res) => {
res.status(404).json({
message: 'Route Not Found'
});
});
// Global error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
message: err.message,
error: {}
});
});
// Set up the port
const port = process.env.PORT || 5000;
const server = app.listen(port, () => console.log('Express app listening on port ' + port + ''));
// Socket Setup
const io = socket(server);
io.on('connection', function(socket) {
console.log('Made socket connection', socket.id);
io.sockets.emit('user');
// Listen for message being sent from client
// Look for message name
// Fire callback and take in the data sent from the client
socket.on('chat', function(data) {
// Emit the event down all socket(s)
console.log('A message is coming into socket:', data.message);
io.sockets.emit('chat', data);
});
socket.on('chatGif', function(data) {
// Emit the event down all socket(s)
console.log('A gif is coming into socket:', data.gif);
io.sockets.emit('chatGif', data);
})
// Handle typing event
socket.on('typing', function(data){
socket.broadcast.emit('typing', data);
});
// Handle stopped typing event
socket.on('stoppedTyping', function(data){
console.log('User stopped typing');
socket.broadcast.emit('stoppedTyping', data);
});
// Alert user disconnect
socket.on('disconnect', function() {
console.log('User is disconnecting', socket.id);
});
});
// Export the App
module.exports = app;