-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.js
More file actions
88 lines (68 loc) · 1.95 KB
/
Copy pathapp.js
File metadata and controls
88 lines (68 loc) · 1.95 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Module dependencies
var fs = require('fs')
, path = require('path')
, util = require('util')
, MusicLibrary = require('./musiclibrary')
, SocketServer = require('./socket-server')
, express = require('express')
var app = module.exports = express.createServer()
, io = require('socket.io').listen(app);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set("view options", {layout: false});
//html rendering
app.register('.html', {
compile: function(str, options){
return function(locals){
return str;
};
}
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'))
})
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})
app.configure('production', function(){
app.use(express.errorHandler())
})
// Routes
app.get('/', function(req, res){
res.render('index.html', {
locals: {
title: 'Streamy client'
}
})
})
//Serve streaming audio - audio src points here
app.get('/stream/:song', function(req, res) {
var songPath = socketServer.musicLibrary.songs[req.params.song]
path.exists(songPath, function (exists) {
if(!exists) {
var msg = 'File `' + songPath + '` not found'
console.log('\nSTREAMY:', msg)
res.writeHead(404)
res.end(msg)
return
}
//stream song to client
fs.stat(songPath, function (err, stats) {
if(err) {
console.log('\nSTREAMY: stat\'ing error:', err)
res.writeHead(500)
return
}
res.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stats.size })
var readStream = fs.createReadStream(songPath)
util.pump(readStream, res) //pump song to client
})
})
})
var PORT = 3000
app.listen(PORT)
console.log("STREAMY: listening on port", PORT)
var socketServer = new SocketServer(io)