使用nodejs和html布局一个简单的视频播放网站,但是使用localhost:端口访问html无法加载视频

js代码:

javascript 复制代码
// app.js
const express = require('express');
const path = require('path');
const app = express();

// 设置静态文件目录,这里假设你的视频文件在public/videos/目录下
app.use(express.static(path.join(__dirname, '')));


// 设置主页路由,指向一个包含HTML5 video元素的页面
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'test.html'));
});

// 启动服务器
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

html代码:

html 复制代码
<!-- public/views/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Video Player</title>
</head>
<body>
    <h1>hello</h1>
  <video width="640" height="360" controls>
    <!-- 视频路径相对于static目录 -->
    <source src="./videos/111.mp4" type="video/mp4">
  </video>

  <img src="./videos/qq1.png" alt="">

  
</body>
</html>

在vscode中直接打开test.html可以正常加载视频。但是使用node 启动js监听端口然后调用test.html就不行,不能加载html文件。

文件目录结构:

--videos

--111.mp4

---qq1.png

--test.js

--test.html

问题,发现是localhost访问时地址不对。浏览器会直接访问localhost:3001/111.mp4

所以这里需要设置路径映射

其实这段代码:

javascript 复制代码
app.use(express.static(path.join(__dirname, '')));

就是用来设置路径映射的,只是最开始没有设置正确。

这里将代码改为:

javascript 复制代码
app.use(express.static(path.join(__dirname, 'videos')));

将路径映射到videos文件夹,然后就可以读取到videos文件夹下的文件。

修改后代码:

js:

javascript 复制代码
// app.js
const express = require('express');
const path = require('path');
const app = express();

// 设置静态文件目录,这里假设你的视频文件在public/videos/目录下
app.use(express.static(path.join(__dirname, 'videos')));


// 设置主页路由,指向一个包含HTML5 video元素的页面
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'test.html'));
});

// 启动服务器
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

html:

html 复制代码
<!-- public/views/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Video Player</title>
</head>
<body>
    <h1>hello</h1>
  <video width="640" height="360" controls>
    <!-- 视频路径相对于static目录 -->
    <source src="111.mp4" type="video/mp4">
  </video>

  <img src="./qq1.png" alt="">

  
</body>
</html>
相关推荐
LinXunFeng21 小时前
Flutter 拖拉对比组件,换装图片前后对比必备
前端·flutter·开源
BD_Marathon21 小时前
【PySpark】安装测试
前端·javascript·ajax
stu_kk21 小时前
Ecology9明细表中添加操作按钮与弹窗功能技术分享
前端·oa
dkgee21 小时前
如何禁止Chrome的重新启动即可更新窗口弹窗提示
前端·chrome
天若有情67321 小时前
新闻通稿 | 软件产业迈入“智能重构”新纪元:自主进化、人机共生与责任挑战并存
服务器·前端·后端·重构·开发·资讯·新闻
香香爱编程21 小时前
electron对于图片/视频无法加载的问题
前端·javascript·vue.js·chrome·vscode·electron·npm
程序猿_极客1 天前
【期末网页设计作业】HTML+CSS+JavaScript 蜡笔小新 动漫主题网站设计与实现(附源码)
前端·javascript·css·html·课程设计·期末网页设计
zl_vslam1 天前
SLAM中的非线性优-3D图优化之轴角在Opencv-PNP中的应用(一)
前端·人工智能·算法·计算机视觉·slam se2 非线性优化
CDwenhuohuo1 天前
用spark-md5实现切片上传前端起node模拟上传文件大小,消耗时间
前端
阿桂有点桂1 天前
React使用笔记(持续更新中)
前端·javascript·react.js·react