'Overwriting request methods in express

I was watching a video about middlewares in express and I saw the guy overwriting get request into post request. I got curious and tried overwriting get request to delete request on my existing piece of code. My code is this:

app.use("/comments/:id/delete", (req, res, next) => {
  req.method = "DELETE";
  next();
});
app.delete("/comments/:id/delete", (req, res) => {
  const { id } = req.params;
  comments = comments.filter((c) => c.id !== id);
  res.redirect("/comments");
});

Note: I didn't followed REST because it was conflicting with show which I had defined earlier.

My EJS:

  <form action="/comments/<%= comment.id %>/delete">
              <button>Delete</button>
</form>

My ejs full image: enter image description here

My server Detail Image: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source