-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (51 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
59 lines (51 loc) · 1.5 KB
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 express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const socketio = require('socket.io');
const http = require('http');
const path = require('path');
require('dotenv').config();
const app = express();
const server = http.createServer(app);
const io = socketio(server, {
cors: {
origin: 'http://localhost:3000',
credentials: true,
},
});
io.on('connection', (socket) => {
socket.on('join', ({ gameId }) => {
socket.join(gameId);
io.to(gameId).emit('userJoined');
});
socket.on('postMove', (data) => {
io.to(data.gameId).emit('receiveMove', data);
});
});
app.use(express.json());
app.use(cors());
// setting the routers
const gameRouter = require('./routes/gameRouter');
const userRouter = require('./routes/userRouter');
app.use('/g', gameRouter);
app.use('/u', userRouter);
// Serve static assets if in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// connect to mongodb database
mongoose.connect(
process.env.MONGO_URI,
{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true },
(err) => {
if (err) throw err;
console.log('MongoDB is connected');
}
);
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server is up and running at port: ${port}`);
});