我的代码如下:
router.get('/image',(req,res,next)=>{
const fileName = "path_to.jpg"
res.sendfile(fileName,(err)=>{
if (err) {
next(err);
} else {
console.log('Sent:', fileName);
}
});
})当按原样与Express 4一起使用时,此路由器的工作方式是成功地将所需映像发送到客户端。但是,服务器会写出一条警告消息,告诉我"res.sendfile“已被弃用,我应该切换到"res.sendFile”(只需将"file“中的"f”大写)。
express deprecated res.sendfile: Use res.sendFile instead当我这样做的时候,我的功能代码不再是200状态,而是500状态。除了"f“变成大写字母之外,没有其他方面发生变化。
我是不是误解了明确的警告要求我做什么?
我引用了( http://expressjs.com/en/api.html#res.sendFile ),没有看到我的代码片段有任何明显的不正确之处...如果还有其他我应该提供的信息,请告诉我。
发布于 2018-09-25 03:59:34
通过在路由器中包含模块" path“,我就能够创建sendFile()要使用的绝对路径,正如@karthick在原始帖子上所做的评论所建议的那样。
因此,在调用sendFile()时,请使用文件的相对路径作为根,并使用由“fileName”为选项“fileName”创建的绝对路径。
下面的示例假设映像文件与路由器脚本位于同一文件夹中。
router.get('/image',(req,res,next)=>{
const fileName = "1.jpg"
res.sendFile(fileName,{ root: path.join(__dirname, './') },(err)=>{
if (err) {
console.log(err)
next(err)
} else {
console.log('Sent:', fileName);
}
});
})https://stackoverflow.com/questions/52485334
复制相似问题