[英]Javascript 404 Resource Not Found NodeJS
I'm running a Node JS server on an old laptop of mine, and I've portforwarded the port 8006 so that I can connect to the node server from another computer and view the web pages. 我正在我的旧笔记本电脑上运行Node JS服务器,并且将端口8006进行了端口转发,以便可以从另一台计算机连接到节点服务器并查看网页。 However, I have a sound file that I want the server to play when something happens, but after checking the console output on my computer, it keeps throwing this error:
但是,我有一个声音文件,我希望服务器在发生某些情况时播放,但是在检查计算机上的控制台输出后,它会不断抛出此错误:
GET http://0.0.0.0:8006/surprise.mp3 404 (Not Found)
GET http://0.0.0.0:8006/surprise.mp3 404(未找到)
Where of course the zeroes are my actual IP address (the ip of the server). 当然零是我的实际IP地址(服务器的ip)。 Everything else runs fine including all the javascript files and the webpages.
一切正常,包括所有javascript文件和网页。 This is the code that actually plays the sound:
这是实际播放声音的代码:
var audio = new Audio('surpise.mp3');
audio.play();
And that's sitting inside one of the client-side javascript files. 这位于客户端javascript文件之一中。 How can I make that particular mp3 file accessible to outside users?
如何使外部用户可以访问该特定mp3文件?
If you're using the Express framework (which it sounds like you are), you can create a .get()
route for /surprise.mp3
and then use res.SendFile("/somepath/surprise.mp3")
so that your node.js server will serve that file when that route is requested. 如果您使用的是Express框架(听起来很像),则可以为
/surprise.mp3
创建.get()
路由,然后使用res.SendFile("/somepath/surprise.mp3")
以便请求该路由时,node.js服务器将提供该文件。
Or, if you have multiple static files to serve, you can use express.static()
to set up a directory of static files that will get automatically served. 或者,如果要提供多个静态文件,则可以使用
express.static()
设置将自动提供服务的静态文件目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.