如何在Node JS上投放mp3文件? [英] How can I serve mp3 file on node js?

查看:341
本文介绍了如何在Node JS上投放mp3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的Web应用程序,如果用户按下按钮,它将发出声音(服务器端的mp3文件).

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");
});

如果我在audio属性中插入在线源,则可以使用它,但是它不提供我的本地服务器文件.我该如何解决?

It works if I insert online source in audio attribute, but it does not serve my local server file. How can I fix this?

提前谢谢!

推荐答案

要提供静态文件(例如图像,CSS文件和JavaScript文件,音频),请使用Express中内置的express.static内置中间件功能.

To serve static files such as images, CSS files, and JavaScript files, audio, use the express.static built-in middleware function in Express.

我已经回答了这类问题.查看此答案- res.sendFile(path.join(__ dirname, '../public','mapv02.html'));

I've already answered such type of question. See this answer - res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));

这篇关于如何在Node JS上投放mp3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆