如何用nodejs显示图片文件

How to display image file with nodejs

我已成功将文件名保存到我的 mongodb 数据库中,但我似乎无法弄清楚如何将图像显示到车把页面。这是我正在使用的代码:

router.get('/showPost/:id',(req,res) => {
Post.findById(req.params.id).then((post) => {
   res.render('showPost',{ post: post})
        
    });

这是我尝试显示图像的方式,但它只显示为损坏的图标。

<img src = {{{post.image}}} >

如有任何帮助,我们将不胜感激!

您 index.js 设置正确了吗?

app.use(express.static('public')) 

创建根文件夹调用 public。把你的图片放在那里

然后做

 <img src="/{{post.image}}>