express - How to implement simple audio playback in Node.js? -


i'm developing simple node.js chat application socket.io , express , play short audio file on button click event. relevant setup follows:

var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http);  app.get('/', function(req, res) { res.sendfile('index.html'); });  http.listen(process.env.port || 5000, function() { console.log("server running on port 5000"); }); 

and relevenat index.html code:

<button onclick="document.getelementbyid('mytune').play()">play music</button> <audio id="mytune">   <source src="sound.mp3"> </audio> 

this works fine on basic html file when run in browser not when run in node. have feeling there's special set required node/express able access sound file i'm not sure exactly. appreciated.

from server code not clear how make mp3 file available browser. try serving static content. add

app.use(express.static(__dirname + '/public'));

and place sound.mp3 public directory


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -