-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
64 lines (50 loc) · 1.78 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
var express = require('express');
var bodyParser = require('body-parser');
var ngrok = require('ngrok');
var googleHomeUrlCaster = require ('./googleHomeUrlCaster');
var app = express();
var serverPort = 3000;
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var riffCount = 90;
googleHomeUrlCaster.getCastDevices();
app.get('/', function (req, res){
console.log('GET /');
res.send('Ping');
});
app.post('/door', urlencodedParser, function (req, res){
var ip = req.body.ip;
console.log('POST /door');
var trackNumber = Math.floor(Math.random() * riffCount) + 1;
var riffUrl = 'https://storage.googleapis.com/seinfeld-riffs/Dan%20Seinfeld%20' + trackNumber + '.mp3';
if(ip) {
googleHomeUrlCaster.castUrl(ip, riffUrl, function(result, err){
if(!err) {
res.send('Played ' + riffUrl + ' on '+ ip);
}
});
} else {
res.status(400).send('Please POST "ip=192.168.1.215"');
}
});
app.post('/url', urlencodedParser, function (req, res){
var ip = req.body.ip;
var url = req.body.url;
console.log('POST /url');
if(ip && url) {
googleHomeUrlCaster.castUrl(ip, url, function(result, err){
if(!err) {
res.send('Played ' + url + ' on ' + ip);
}
});
} else {
res.status(400).send('Please POST "ip=192.168.1.215&url=https://s3-us-west-2.amazonaws.com/blog-spokeo-test/seinfeld.mp3"');
}
});
app.listen(serverPort, function() {
ngrok.connect(serverPort, function (err, url){
console.log('Listening at http://localhost:' + serverPort);
console.log('Available at ' + url);
console.log('example:');
console.log('curl -X POST -d "ip=192.168.1.215" ' + url + '/door');
})
});