20

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?

3

2 Answers 2

44

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 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());
4
  • It is possible to use app.use(express.urlencoded()); and app.use(express.methodOverride()); bodyParser adds support for file uploading which may not be desireable. Express docs has more details.
    – yanychar
    Feb 9, 2014 at 18:55
  • actually express.bodyParser() is deprecated in versions 3.4 of Express and 2.9 of Connect. There is security issues on the use of express.bodyParser() explained here
    – netusco
    Mar 21, 2014 at 22:54
  • I think this is the article @ErnestConill is referring to:
    – cbaigorri
    Apr 9, 2014 at 19:13
  • I'm trying first way, but its not working, by default POST method is calling. This is my code, kopy.io/FCPAu Jun 26, 2017 at 16:02
2

actually express.bodyParser() is deprecated in versions 3.4 of Express and 2.9 of Connect. There is security issues on the use of express.bodyParser() explained here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.