How do i get expressjs to use the delete and put methods for form?
<form method="DELETE" action="">
Using the above is sending a GET request in latest stable version of chrome. Is this supposed to be a browser issue?
Is there a better way to override this without having a special input field for supporting these?
Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It allows for a specific type of polymorphism (subtyping).
Koa, React, Flask, Django, and Golang are the most popular alternatives and competitors to ExpressJS.
Express has not been updated for years, and its next version has been in alpha for 6 years. People may think it is not updated because the API is stable and does not need change. The reality is: Express does not know how to handle async/await .
You just need to set the form to post, then create a hidden field like
<input type="hidden" name="_method" value="delete"/>
And set the configuration, according to the express version you are using. Then the form method will be overridden by the value of that hidden field.
The latest version of express.js will require you to install the method-override package, then configure your app like this:
var methodOverride = require('method-override')
app.use(methodOverride('_method'));
Old versions might use:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.methodOverride());
An even older usage was:
app.use(express.bodyParser());
app.use(express.methodOverride());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With