我对路由参数有一个奇怪的问题。我正在尝试制作一个简单的视频流系统,在其中将视频的 id(使用路由参数)传递到管道流中,然后将实际视频流回。
视频确实流回了,但我收到“传入的参数必须是 12 个字节的单个字符串或 24 个十六进制字符的字符串”错误,这会破坏整个 html 页面。
如果有人可以帮助指导我如何解决这个问题的正确方向,那将不胜感激!
视频获取路线
app.get('/v/:id', (req, res)=> {
const videoId = mongoose.mongo.ObjectID(req.params.id)
gfs.files.findOne({_id: videoId}, (err, file)=> {
if(err) {
throw err
} else if(!file) {
res.redirect('/')
} else if(file) {
res.render('Video.ejs', {file: file, title: "Video :: Clipit"})
}
})
})
视频管道流路由
app.get('/api/stream/:filename', (req, res)=> {
gfs.files.findOne({filename: req.params.filename}, (err, file)=> {
if(err) {
throw err
} else {
const readstream = gfs.createReadStream(file.filename)
readstream.pipe(res)
}
})
})
视频 HTML 文件 (EJS)
<!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">
<%- include('./partials/Settings.ejs') %>
<title><%= title %></title>
</head>
<body>
<header>
<%- include('./partials/Header.ejs') %>
</header>
<main>
<div>
<video id="videoPlayer" width="650" controls autoplay>
<source src="/api/stream/<%= file.filename %>" type="<%= file.contentType %>"/>
</video>
</div>
</main>
</body>
</html>
错误
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (C:\Users\gabri\Desktop\clipit\node_modules\bson\lib\bson\objectid.js:59:11)
at Function.ObjectID (C:\Users\gabri\Desktop\clipit\node_modules\bson\lib\bson\objectid.js:40:43)
at C:\Users\gabri\Desktop\clipit\server.js:66:36
at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:354:14)
at param (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:410:3)
at next (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\gabri\Desktop\clipit\node_modules\body-parser\lib\types\urlencoded.js:91:7)
at Layer.handle [as handle_request] (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:317:13)
at C:\Users\gabri\Desktop\clipit\node_modules\express\lib\router\index.js:284:7
(node:7784) DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead
注意:我使用 GridFS Storage 存储视频,使用 GridFS Stream 流式传输视频。提前致谢!