I have been following this great tutorial on YouTube trying to make a website. However once I deployed it, the images aren't showing because currently they are all saved in a local folder called uploads. This is how it looks like right now:
SERVER SIDE:
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}_${file.originalname}`)
},
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname)
if (ext !== '.jpg' || ext !== '.png') {
return cb(res.status(400).end('only jpg, png are allowed'), false);
}
cb(null, true)
}
})
var upload = multer({ storage: storage }).single("file")
router.post("/uploadImage", auth, (req, res) => {
upload(req, res, err => {
if(err) return res.json({success: false, err})
return res.json({ success: true, image: res.req.file.path, fileName: res.req.file.filename})
})
});
CLIENT SIDE: image field stores a string (i.e. "uploads\xxxxxxxx.jpg")
function ProductImage(props) {
const [Image, setImage] = useState();
useEffect(() => {
setImage(`http://localhost:5000/${props.detail.image}`)
}, [props.detail])
return(
<div>
<img style={{maxWidth: '30vw'}}
src={Image} alt="productImg"/>
</div>
)
}
So I guess my question now is that how do I go about actually uploading the images to MongoDB, and how would I retrieve them?