Add remove route to delete an image
This commit is contained in:
parent
34c328214e
commit
ea770901cb
2 changed files with 19 additions and 0 deletions
2
app.js
2
app.js
|
@ -10,6 +10,7 @@ let indexRoute = require("./routes/index");
|
||||||
let uploadRoute = require("./routes/upload");
|
let uploadRoute = require("./routes/upload");
|
||||||
let loginRoute = require("./routes/login");
|
let loginRoute = require("./routes/login");
|
||||||
let imagesRoute = require("./routes/images");
|
let imagesRoute = require("./routes/images");
|
||||||
|
let removeRoute = require("./routes/remove");
|
||||||
let notFoundRoute = require("./routes/notFound");
|
let notFoundRoute = require("./routes/notFound");
|
||||||
let errorRoute = require("./routes/error");
|
let errorRoute = require("./routes/error");
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ app.use(morgan("dev"))
|
||||||
.use("/upload", uploadRoute)
|
.use("/upload", uploadRoute)
|
||||||
.use("/login", loginRoute)
|
.use("/login", loginRoute)
|
||||||
.use("/images", imagesRoute)
|
.use("/images", imagesRoute)
|
||||||
|
.use("/remove", removeRoute)
|
||||||
.use(notFoundRoute)
|
.use(notFoundRoute)
|
||||||
.use(errorRoute)
|
.use(errorRoute)
|
||||||
.listen(process.env.PORT || 8080);
|
.listen(process.env.PORT || 8080);
|
||||||
|
|
17
routes/remove.js
Normal file
17
routes/remove.js
Normal 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;
|
||||||
|
|
Loading…
Reference in a new issue