Node.js 为什么我看不到用户multer之后的图像?

Node.js 为什么我看不到用户multer之后的图像?,node.js,reactjs,multer,Node.js,Reactjs,Multer,我使用multer上传个人资料图片。 完成并获得路径后,我将图像路径放在img src元素中,但在控制台上出现错误: Failed to load resource: the server responded with a status of 404 (Not Found) 镜像元素在客户端,我正在使用React。 我的img元素看起来像: <img class="round-img" src="http://localhost:5000/public/en

我使用multer上传个人资料图片。 完成并获得路径后,我将图像路径放在img src元素中,但在控制台上出现错误:

Failed to load resource: the server responded with a status of 404 (Not Found)
镜像元素在客户端,我正在使用React。 我的img元素看起来像:

<img class="round-img" src="http://localhost:5000/public/engage.jpg" alt="">
我的上传路线:

router.post('/upload' ,[auth, upload.single('imageProfile')], async  (req,res) =>{
    try{
        const url = req.protocol + '://' + req.get('host')
        let user= await User.findById(req.user.id);
        const profile = await Profile.findOne({user:req.user.id});
        console.log("The user is : " + user)
        //Update
        if(user)
        {
            user.avatar=url+ '/public/' + req.file.filename
            await user.save();
            return res.json(profile)
        }
    }
    catch(err){
        console.log(err)
    }
})
server.js:

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

我做错了什么?

您有一个自定义的
文件名
函数,可以将原始文件名(如“my file name.png”)转换为“my file name”(注意文件扩展名的丢失)。因此,您需要确保从html中请求此确切的文件名:

<img class="round-img" src="http://localhost:5000/public/my-file-name">


如果要保留文件扩展名,已经有一个问题解决了此问题:

通过在my server.js中添加此行解决了此问题:

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

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

很好,但是我很确定你的代码对你动态上传的图片不起作用。