Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expressjs support for method delete and put without the methodoverride

Tags:

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?

like image 699
Amit Avatar asked Mar 25 '12 09:03

Amit


People also ask

What is ExpressJS method override?

Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.

What does method override do?

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).

What is the alternative of ExpressJS?

Koa, React, Flask, Django, and Golang are the most popular alternatives and competitors to ExpressJS.

Is ExpressJS outdated?

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 .


1 Answers

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());
like image 113
Pedro Luz Avatar answered Nov 09 '22 23:11

Pedro Luz