Last active
April 28, 2017 08:20
-
-
Save WORMSS/42d7b4c1517f85c3bf725c0c0ed46d75 to your computer and use it in GitHub Desktop.
Express MethodOverride
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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