简体   繁体   English

Node.js将html启动到localhost:8000,如何在该html文件中调用.mp3 / .mp4文件

[英]Node.js launches html to localhost:8000, how can I call a .mp3/.mp4 file in that html file

I am using node.js to launch an HTML file to display on the localhost:8000. 我使用node.js启动HTML文件以显示在localhost:8000上。
None of the media files that the HTML calls work. HTML调用的所有媒体文件都不起作用。 The HTML works fine when I launch it from the windows icon. 当我从Windows图标启动它时,HTML工作正常。 I assume that I need to change how the media files are called. 我假设我需要更改媒体文件的调用方式。

I have tried moving the media files into the native folder. 我已经尝试将媒体文件移动到本机文件夹中。

//This is how I call the HTML.

var http = require('http'),
fs = require('fs');



fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err; 
    }       
    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(8000);
 });


 //This is how the media file is called in the HTML (index.html).

 <audio id="Whistle" src="C:\Site\Whistle.mp3" preload="auto" ></audio>

There is javascript in the HTML file that "plays" the file. HTML文件中有javascript“播放”文件。

I am not getting any error messages from node.js or the web browser. 我没有从node.js或Web浏览器收到任何错误消息。 It just isn't calling the file and moving on. 它只是没有调用文件并继续前进。

I am not getting any error messages from node.js or the web browser 我没有从node.js或Web浏览器收到任何错误消息

That's surprising because C:/... isn't a valid path. 这是令人惊讶的,因为C:/...不是一个有效的路径。 Rember that it is resolved on the client side , and there you don't have access to the servers file system. 请记住它已在客户端解析,并且您无法访问服务器文件系统。 You also don't have access to the local file system the browser is running on for security reasons. 出于安全原因,您也无权访问运行浏览器的本地文件系统。

For sure you could send a request to the server instead, then the server could answer with a sound file. 您肯定可以向服务器发送请求 ,然后服务器可以使用声音文件进行回答。

Read on 继续阅读

Also make sure you know the basics 还要确保您了解基础知识

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM