NodeJS:无法加载媒体,可能是因为服务器或网络出现故障,或者是格式不受支持
NodeJS : The media could not be loaded, either because the server or network failed or because the format is not supported
我有一个 NodeJS 服务器,我想提供本地视频,但是在将 link 转到 html 页面时出现错误,上面写着:无法加载媒体,可能是因为服务器或网络出现故障,或者因为格式不受支持。
我在 Safari 和 Chrome 上试过,但 none 似乎可以使用它。
所以这是我的代码:
index.js
const express = require("express");
const app = express();
app.get("/", function(req, res) {
console.log("GET \"/\" requested.");
res.sendFile(__dirname + "/Public/index.html");
});
app.get("/videotest", function(req, res) {
console.log("GET \"/videotest\" requested.")
res.sendFile(__dirname + "/Public/vtest.html")
});
app.listen(3000, function() {
console.log("Server listening on port 3000")
});
vtest.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Video Player</title>
<link href="https://unpkg.com/video.js@7/dist/video-js.min.css" rel="stylesheet"/>
<!-- City -->
<link href="https://unpkg.com/@videojs/themes@1/dist/city/index.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/solid.min.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<video class="vjs-custom video-js vjs-big-play-centered" width="960" height="540" data-setup='{}' controls>
<source src="videos/video.mp4" type="video/mp4"/>
</video>
<script src="https://vjs.zencdn.net/7.10.2/video.js"></script>
</body>
</html>
这是 Public 文件夹树:
注意:我尝试在不启动 Node 服务器的情况下阅读视频,它运行良好,但不是来自 Node 本地主机。
实际上我在我的代码中发现我只是忘记使用 app.use(express.static('public');
提供静态文件
我有一个 NodeJS 服务器,我想提供本地视频,但是在将 link 转到 html 页面时出现错误,上面写着:无法加载媒体,可能是因为服务器或网络出现故障,或者因为格式不受支持。
我在 Safari 和 Chrome 上试过,但 none 似乎可以使用它。
所以这是我的代码:
index.js
const express = require("express");
const app = express();
app.get("/", function(req, res) {
console.log("GET \"/\" requested.");
res.sendFile(__dirname + "/Public/index.html");
});
app.get("/videotest", function(req, res) {
console.log("GET \"/videotest\" requested.")
res.sendFile(__dirname + "/Public/vtest.html")
});
app.listen(3000, function() {
console.log("Server listening on port 3000")
});
vtest.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Video Player</title>
<link href="https://unpkg.com/video.js@7/dist/video-js.min.css" rel="stylesheet"/>
<!-- City -->
<link href="https://unpkg.com/@videojs/themes@1/dist/city/index.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/solid.min.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<video class="vjs-custom video-js vjs-big-play-centered" width="960" height="540" data-setup='{}' controls>
<source src="videos/video.mp4" type="video/mp4"/>
</video>
<script src="https://vjs.zencdn.net/7.10.2/video.js"></script>
</body>
</html>
这是 Public 文件夹树:
注意:我尝试在不启动 Node 服务器的情况下阅读视频,它运行良好,但不是来自 Node 本地主机。
实际上我在我的代码中发现我只是忘记使用 app.use(express.static('public');