/
index.js
43 lines (35 loc) · 1.23 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const io = require('socket.io')();
const keys = require('./config/keys');
require('./models/Pool');
const { updatePool, fetchPool } = require('./controller/db-controller');
mongoose.Promise = global.Promise;
mongoose.connect(keys.MONGODB_URI).then(
() => console.log('MongoDB Connected!!$%&@!'),
err => console.log('Error connecting to MongoDB', err),
);
const app = express();
app.use(bodyParser.json());
app.get('/api/fetchPool', fetchPool);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
io.listen(server);
console.log('Socket connected.');
io.on('connection', (client) => {
console.log('Made socket connection with client: ', client.id);
// Handle websocket events
client.on('updatePool', (data) => {
console.log(data);
updatePool(data);
io.sockets.emit('updatePool', data);
});
});