How can I serve mp3 file on node js?

只谈情不闲聊 提交于 2019-12-12 21:24:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!