我将我的应用程序部署在具有mLab数据库的Heroku上,但看不到图像。我的应用程序可在此处在线使用:https://evening-bayou-56962.herokuapp.com/
我图像的源代码如下
<img class="header-img" src="https://evening-bayou-56962.herokuapp.com/uploads/aefd5767e35894a025bd6fac4f252d3d.jpg" alt="vincent-peillon">
在我的后端,这是我添加带有图像的条目的方法:
router.post("/add", upload.single("image"), (req, res) => {
// On rename la photo dans le upload
const data = JSON.parse(req.body.data);
// console.info(data);
const extension = getExtension(req.file); // Voir en dessous
const filename = req.file.filename + extension;
const serverPictureName = "public/uploads/" + filename;
const apiPictureName = "uploads/" + filename;
fs.rename(req.file.path, serverPictureName, function(err) {
if (err) {
console.log("il y a une erreur", err);
return res
.status(400)
.json({ img: "L'image n'a pas pu être sauvegardée" });
}
Deputy.findOne({ name: data.name }).then(deputy => {
if (deputy) {
return res.status(400).json({ name: "Ce député existe déjà" });
} else {
// Delete the @ to be saved in DB and better diplay in front
console.log("data.twitter", data.twitter);
while (data.twitter.charAt(0) === "@") {
data.twitter = data.twitter.substr(1);
}
console.log("data.twitter", data.twitter);
const newDeputy = new Deputy({
firstName: data.firstName,
surname: data.surname,
name: data.firstName + " " + data.surname,
mandateFrom: data.mandateFrom || "",
mandateTo: data.mandateTo || "",
group: data.group || "",
party: data.party || "",
twitter: data.twitter || "",
picture: apiPictureName || "",
slug: slug(data.name.toString())
});
newDeputy
.save()
.then(user => res.json(user))
.catch(err => console.log("err", err));
}
});
});
});
这在本地完美运行。