问题描述
让我开始为我的头衔道歉。我遇到了这个奇怪的 MongoDB 错误,我真的不知道给这篇文章取什么名字。
我正在尝试使用 GridFS-Stream 制作一个简单的视频流平台。流式传输实际视频不是问题。一旦我转到视频所需的链接,它就会播放数据库中的视频。但问题是即使在MongoDB中找到了视频,node.js仍然认为没有找到任何东西。
我真的无语了。我一直在互联网上到处搜索,这几天一直被这个错误所困扰。
如果有人能帮助我找到解决此错误的正确方向,我们将非常感激不尽。
控制台日志(请原谅警告)
Clipit Webserver | v1.0.0
----------------------------------------------
> Server is up and running
(node:16292) [MONGODB DRIVER] Warning: Top-level use of w,wtimeout,j,and fsync is deprecated. Use writeConcern instead.
(node:16292) [MONGODB DRIVER] Warning: Current Server discovery and Monitoring engine is deprecated,and will be removed in a future version. To use the new Server discover and Monitoring engine,pass option { useUnifiedTopology: true }
to the MongoClient constructor.
> Succesfully connected to the database
127.0.0.1 has connected to: 60df1dbe017d5035d834b5ae
Video not found
(node:16292) DeprecationWarning: GridStore is deprecated,and will be removed in a future version. Please use GridFSBucket instead
> 127.0.0.1 has connected to: /
注意它是如何首先找到视频甚至发送视频 ID 的?我不知道为什么第二次找到视频,它说找不到视频。我完全不知道这个错误是什么以及如何修复它。
视频路由和管道
//@ROUTE: Video route
//@DESCRIPTION: using the stream route,finds the video in the database and streams the video to the client
app.get('/v/:id',(req,res)=> {
const videoId = req.params.id
gfs.files.findOne({'test': videoId},(err,file)=> {
if(err) {
throw err
}
if(file) {
console.log(`${req.ip} has connected to: ${file._id}`)
res.render('Video.ejs',{file: file,title: "Video :: Clipit",curSession: req.session})
} else {
console.log('Video not found')
res.redirect('/')
}
})
})
//@ROUTE: Video stream route
//@DESCRIPTION: Finds the video in the database,and creates a pipe stream,used in the video route
app.get('/api/stream/:filename',res)=> {
gfs.files.findOne({filename: req.params.filename},file)=> {
if(err) {
throw err
} else {
const readstream = gfs.createReadStream(file.filename)
readstream.pipe(res)
}
})
})
视频的 HTML 页面
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta http-equiv="X-UA-Compatible" content="IE=edge">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<title><%= title %></title>
</head>
<body>
<header>
<div class="clipit-title">
<h3>Clipit</h3>
</div>
<div class="clipit-routes">
<ul class="nav-links">
<li><a href="/" class="nav-link">Upload</a></li>
<% if(curSession.loggedIn == true) { %>
<li><a href="#" class="nav-link"><%= curSession.username %></a></li>
<li><a href="/api/logout" class="nav-link">Log out</a></li>
<% } else { %>
<li><a href="/login" class="nav-link">Log in</a></li>
<li><a href="/signup" class="nav-link">Sign up</a></li>
<% } %>
</ul>
</div>
</header>
<main>
<div>
<video id="videoPlayer" width="650" controls autoplay>
<source src="/api/stream/<%= file.filename %>" type="<%= file.contentType %>"/>
</video>
</div>
</main>
</body>
</html>
GridFS 存储和上传
//Sets up the GridFS stream
conn.once('open',()=> {
gfs = Grid(conn.db,mongoose.mongo)
gfs.collection(settings.BUCKET)
})
//Creates the storage for the videos (used for storing video data when uploading a video)
const videoStorage = new GridFsstorage({
url: settings.DBURI,file: (req,file)=> {
return new Promise((resolve,reject)=> {
crypto.randomBytes(16,buf)=> {
if(err) {
return reject(err)
}
const fileName = buf.toString('hex') + path.extname(file.originalname)
const fileInfo = {
filename: fileName,bucketName: settings.BUCKET
}
resolve(fileInfo)
})
})
}
})
//Creates the upload for the video storage
const videoUpload = multer({storage: videoStorage})
感谢您给我时间,我希望你们中的一位能帮助我。 祝你有美好的一天!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)