Skip to content

Instantly share code, notes, and snippets.

@WORMSS
Last active April 28, 2017 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WORMSS/42d7b4c1517f85c3bf725c0c0ed46d75 to your computer and use it in GitHub Desktop.
Save WORMSS/42d7b4c1517f85c3bf725c0c0ed46d75 to your computer and use it in GitHub Desktop.
Express MethodOverride
const Express = require("express");
const methodOverride = require("method-override");
const app = new Express();
const router = new Express.Router();
//app.use(methodOverride("_method", {"methods": ["POST", "GET"]})); // Works too.
router.use(methodOverride("_method", {"methods": ["POST", "GET"]})); // on route so only the following use the method override.
router.get("/:id", all("get"));
router.post("/:id", all("post"));
router.delete("/:id", all("delete"));
router.put("/:id", all("put"));
router.patch("/:id", all("patch"));
app.get("/", index);
app.use("/test", router);
function index(req, res) {
// Change for testing
res.send(`<form action="/test/123?_method=delete" method="post"><input type="submit"></form>`);
}
function all(method) {
return (req, res) => {
res.send(`<a href="/">back</a> method: ${method} id: ${req.params.id}`);
}
}
require("http").createServer(app).listen(3001, () => console.log("server started"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment