问题
I am building simple web app that if the user press button, it will make sound(mp3 file in the server side).
//html file
<script>
function playSound () {
document.getElementById('play').play();
}
</script>
<body>
<audio id='play', src='http://127.0.0.1:1337/', type='audio/mpeg'
<button onclick='playSound()' class='btn btn-danger btn-circle btn-lg'>
<i class='fa fa-5x fa-fire'>
</body>
//app.js
app.get('/music.mp3', function(req, res){
ms.pipe(req, res, "/music.mp3");
});
It works if I insert online source in audio
attribute, but it does not serve my local server file. How can I fix this?
Thanks in advance!
回答1:
To serve static files such as images, CSS files, and JavaScript files, audio, use the express.static built-in middleware function in Express.
I've already answered such type of question. See this answer - res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));
来源:https://stackoverflow.com/questions/36957959/how-can-i-serve-mp3-file-on-node-js