Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created April 6, 2011 18:42
Show Gist options
  • Save tanepiper/906240 to your computer and use it in GitHub Desktop.
Save tanepiper/906240 to your computer and use it in GitHub Desktop.
Media streaming app in expressjs. It reads a directory of music files and outputs an audio tag pointing to it, and serving the file up as the correct OGG media content
fs = require 'fs'
path = require 'path'
util = require 'util'
express = require 'express'
app = module.exports = express.createServer()
app.get '/', (req, res, next) ->
music_path = path.join __dirname, 'music'
fs.readdir music_path, (error, files) ->
output = []
for file in files
output.push "<audio controls preload=\"auto\" autobuffer src=\"http://192.168.253.142:9001/play/#{file}\"></audio>"
output.push '<br />'
res.send output.join('\n'), {"content-type": "text/html"}, 200
app.get '/play/:file', (req, res, next) ->
filePath = path.join __dirname, 'music', req.param 'file'
stat = fs.statSync filePath
res.header 'content-type', 'audio/ogg'
res.header 'content-length', stat.size
res.sendfile filePath
app.listen 9001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment