forked from teemow/playr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (72 loc) · 1.91 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express')
, sys = require('sys')
, urls = require('url')
, rhythmbox = require(__dirname + '/lib/rhythmbox').createClient()
, app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//rhythmbox.scan()
// Routes
app.get('/', function(req, res) {
res.render('index.jade', {
locals: {
title: 'Playr'
}
})
})
app.get('/play-pause', control)
app.get('/next', control)
app.get('/prev', control)
app.get('/info', control)
function control(req, res) {
res.header('Content-Type', 'text/javascript');
function onChange(err, data) {
// error of some sort
if (err) res.send('0')
else {
// info actually requires us returning something useful
if (req.url.match(/^\/info/)) {
res.send(req.query.callback+"(" + JSON.stringify(data) + ")");
} else {
res.send(req.query.callback+"()");
}
}
}
var url = urls.parse(req.url)
, params = url.pathname.match(/^\/(.*)/)
switch (params[1]) {
case "play-pause":
rhythmbox.toggle(onChange)
break;
case "next":
rhythmbox.next(onChange)
break;
case "prev":
rhythmbox.prev(onChange)
break;
default:
rhythmbox.current(onChange)
break;
}
}
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port)
}