-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (70 loc) · 2.58 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
72
73
74
75
76
77
78
79
80
81
82
83
84
var express = require('express');
// Set up app and socket.io server
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var path = require('path');
app.set('port', (process.env.PORT || 3001));
// Database set up
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
if (process.env.NODE_ENV === 'production') {
mongoose.connect(process.env.MONGO_URL);
} else {
mongoose.connect('localhost/auxq');
}
var api = require('./routes/api');
app.use('/api', api);
// Only serve static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', function (req, res){
res.sendFile(path.resolve(__dirname, 'client/build', 'index.html'));
});
}
var Song = require('./models/Song');
var SongQueue = require('./models/SongQueue');
// Sockets!
io.sockets.on('connection', function(socket) {
// Once a client has connected, we expect to get a ping from them saying what queue they want to join
socket.on('joinQ', function(q) {
socket.join(q);
});
// When we receive a ping from a client telling to add a song, we update the database then send the data back through Sockets
socket.on('song:add', function(qId, data) {
// Create a Song then update the appropriate SongQueue
SongQueue.count({_id: qId}, function(err, count) {
if (count > 0) {
Song.create({
title: data.title,
artist: data.artist,
yt_id: data.yt_id
}, function(err, song) {
SongQueue.update(
{_id: qId},
{$push: {'songs': song}},
{upsert: true},
function(err, data) {
if (!err) {
// We've created the song and it has been inserted, let's ping the data back to the client through Sockets
io.sockets.in(qId).emit('song:add', song);
}
});
});
}
});
});
// When we receive a ping from a client to delete a song, we update the database and send the data back through Sockets
socket.on('song:remove', function(qId, qKey, _id) {
SongQueue.findOneAndUpdate({_id: qId, masterKey: qKey}, {$pull: {songs: _id}}, function(err, data) {
if (!err && data !== null) {
io.sockets.in(qId).emit('song:remove', _id);
}
})
});
});
server.listen(app.get('port'), () => {
console.log('Server started at http://localhost:' + app.get('port'));
});