Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 39 additions & 46 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,81 @@
// Module dependencies
const fs = require('fs')

var fs = require('fs')
, path = require('path')
, util = require('util')
const SocketServer = require('./socket-server')

, MusicLibrary = require('./musiclibrary')
, SocketServer = require('./socket-server')
const express = require('express')

, express = require('express')

var app = module.exports = express.createServer()
, io = require('socket.io').listen(app);
const app = module.exports = express.createServer()
const io = require('socket.io').listen(app)

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set("view options", {layout: false});
app.configure(function () {
app.set('views', __dirname + '/views')
app.set('view options', {layout: false})

//html rendering
// html rendering
app.register('.html', {
compile: function(str, options){
return function(locals){
return str;
};
compile: function (str, options) {
return function (locals) {
return str
}
}
});
})

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(app.router)
app.use(express.static(__dirname + '/public'))
})

app.configure('development', function(){
app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})

app.configure('production', function(){
app.configure('production', function () {
app.use(express.errorHandler())
})

// Routes

app.get('/', function(req, res){
app.get('/', function (req, res) {
res.render('index.html', {
locals: {
title: 'Streamy client'
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)
// Serve streaming audio - audio src points here
app.get('/stream/:song', function (req, res) {
const songPath = socketServer.musicLibrary.songs[req.params.song]
const msg = `File '${songPath || req.params.song}' not found`
if (!songPath) return res.end(msg)
fs.exists(songPath, function (exists) {
if (!exists) {
console.log(`\nSTREAMY: ${msg}`)
res.writeHead(404)
res.end(msg)
return
return res.end(msg)
}

//stream song to client
// stream song to client
fs.stat(songPath, function (err, stats) {
if(err) {
console.log('\nSTREAMY: stat\'ing error:', err)
res.writeHead(500)
return
if (err) {
console.log(`\nSTREAMY: stat\'ing error: ${err}`)
return res.writeHead(500)
}

res.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stats.size })
var readStream = fs.createReadStream(songPath)
const readStream = fs.createReadStream(songPath)

util.pump(readStream, res) //pump song to client
readStream.pipe(res) // pump song to client
})
})
})

var PORT = 3000
const PORT = 3000 || process.env.PORT

app.listen(PORT)
console.log("STREAMY: listening on port", PORT)
app.listen(PORT, console.log(`STREAMY: listening on port ${PORT}`))

var socketServer = new SocketServer(io)
const socketServer = new SocketServer(io)
28 changes: 14 additions & 14 deletions musiclibrary.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var EventEmitter = require('events').EventEmitter
, path = require('path')
, util = require('util')
const EventEmitter = require('events').EventEmitter
const path = require('path')
const util = require('util')

, findit = require('findit')
const findit = require('findit')

var FORMATS = ['.mp3', '.m4a'] //supported formats
const FORMATS = ['.mp3', '.m4a'] // supported formats

module.exports = MusicLibrary

Expand All @@ -21,8 +21,8 @@ module.exports = MusicLibrary
function MusicLibrary (opts) {
EventEmitter.call(this)

if(!opts) opts = {}
else if(typeof opts !== 'object') throw new Error('MusicLibrary opts must be of type object')
if (!opts) opts = {}
else if (typeof opts !== 'object') throw new Error('MusicLibrary opts must be of type object')

this.opts = opts
this.songs = {}
Expand All @@ -32,14 +32,14 @@ function MusicLibrary (opts) {
util.inherits(MusicLibrary, EventEmitter)

MusicLibrary.prototype.populate = function () {
var self = this
, root = this.opts.root || path.join(__dirname, '/public/music')
, finder = findit.find(root)
const self = this
const root = this.opts.root || path.join(__dirname, '/public/music')
const finder = findit.find(root)

// cache music library for socket connections
finder.on('file', function(fpath, stat) {
finder.on('file', function (fpath, stat) {
var ext = path.extname(fpath)
if(FORMATS.indexOf(ext) !== -1 && stat.size) {
if (FORMATS.indexOf(ext) !== -1 && stat.size) {
var songTitle = path.basename(fpath, ext)

self.songs[songTitle] = fpath
Expand All @@ -49,4 +49,4 @@ MusicLibrary.prototype.populate = function () {
finder.on('end', function () {
self.emit('ready')
})
}
}
Loading