I have a route which accepts DELETE requests, and I know that with Express you can add
<input type="hidden" name="_method" value="delete" />
to a form that sends a POST request to the url.
However, how would you do this with a link instead of a form?
This is not supported for GET requests:
methodOverride()
only checks req.body
(POST arguments) and request headers - neither can be set for a regular link (you can however set custom headers in AJAX requests even if they use GET).
This make senses since otherwise it could be a major issue even when using CSRF tokens. You can never know when a browser will decide to prefetch a link - so GET requests should never perform actions such as deleting things.
If you really need it and do not care about the drawbacks, consider writing a custom function:
function methodOverrideGET(key) {
key = key || "_method";
return function methodOverrideGET(req, res, next) {
if (req.originalMethod != req.method) {
// already overridden => do not override again
next();
return;
}
req.originalMethod = req.method;
if (req.query && key in req.query) {
req.method = req.query[key].toUpperCase();
delete req.query[key];
}
next();
};
};
Now you can .use(methodOverrideGET)
after .use(methodOverride)
and then simply add _method=DELETE
to the query string.