Add remove route to delete an image

This commit is contained in:
Ethanell 2020-04-27 21:09:43 +02:00
parent 34c328214e
commit ea770901cb
2 changed files with 19 additions and 0 deletions

2
app.js
View file

@ -10,6 +10,7 @@ let indexRoute = require("./routes/index");
let uploadRoute = require("./routes/upload");
let loginRoute = require("./routes/login");
let imagesRoute = require("./routes/images");
let removeRoute = require("./routes/remove");
let notFoundRoute = require("./routes/notFound");
let errorRoute = require("./routes/error");
@ -42,6 +43,7 @@ app.use(morgan("dev"))
.use("/upload", uploadRoute)
.use("/login", loginRoute)
.use("/images", imagesRoute)
.use("/remove", removeRoute)
.use(notFoundRoute)
.use(errorRoute)
.listen(process.env.PORT || 8080);

17
routes/remove.js Normal file
View file

@ -0,0 +1,17 @@
let router = require("express").Router();
let isAuth = require("../user").isAuth;
let fs = require("fs");
router.get("/:name", isAuth, (req, res, next) => {
if ("name" in req.params && fs.existsSync("./images/"+req.params.name)) {
fs.unlinkSync("./images/"+req.params.name);
res.redirect("/");
} else {
next();
}
});
module.exports = router;