-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (61 loc) · 1.69 KB
/
app.js
File metadata and controls
74 lines (61 loc) · 1.69 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var express = require('express'),
cons = require('consolidate'),
app = express(),
spotify = require('node-spotify')({ appkeyFile: 'spotify_appkey.key' });
/* GLOBALS */
var playlists;
var playlist
var tracks;
var track;
var logged_in = false;
// assign the mustache engine to .html files
app.engine('html', cons.mustache);
// set .html as the default extension
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
// test mustache
app.get('/', function(req, res){
if(logged_in){
res.redirect('playlists');
}else{
res.render('login', '');
}
});
app.get('/playlists', function(req, res){
var pl = req.param('index');
console.log(pl);
if(pl){
playlist = playlists[pl];
tracks = playlist.getTracks();
res.render('tracks', {'tracks': tracks, 'playlist': playlist})
}else{
playlists = spotify.getPlaylists();
for(var i = 0; i < playlists.length; i++){
playlists[i]['index'] = i;
}
res.render('playlists', {'playlists': playlists});
}
});
app.post('/play', function(req, res){
console.log("Received body data:");
console.log(req.body.track.toString());
var track = spotify.createFromLink(req.body.track);
spotify.player.play(track);
});
app.post('/stop', function(req, res){
console.log("stopping music");
spotify.player.stop();
res.send("music stopped");
});
app.post('/login', function(req, res){
var user = req.body.user;
var password = req.body.password;
spotify.login(user, password, true, false);
spotify.ready(function(){
logged_in = true;
res.redirect('../');
})
});
app.listen(4000);