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?

Amit
  • 3,952
  • 7
  • 46
  • 80
  • 2
    delete and put generally only work through XMLHttpRequests, not through regular form submissions like you're trying to do. see: http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers – Jonathan Ong Apr 05 '12 at 02:57
  • 1
    expressjs and nodejs use REST architecture. he needs to use PUT and/or DELETE – Pedro Luz Apr 05 '12 at 13:50
  • For express 4 - you will find the answer here. [http://stackoverflow.com/questions/24019489/node-js-express-4-x-method-override-not-handling-put-request?answertab=votes#answer-24020025][1] [1]: http://stackoverflow.com/questions/24019489/node-js-express-4-x-method-override-not-handling-put-request?answertab=votes#answer-24020025 – Johan Hallager Oct 16 '14 at 18:06

2 Answers2

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());
Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
Pedro Luz
  • 2,694
  • 4
  • 44
  • 54
  • 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](http://expressjs.com/api.html#bodyParser) has more details. – yanychar Feb 09 '14 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 '14 at 22:54
  • I think this is the [article](http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html) @ErnestConill is referring to: – cbaigorri Apr 09 '14 at 19:13
  • I'm trying first way, but its not working, by default POST method is calling. This is my code, https://kopy.io/FCPAu – Bhaumik Pandhi Jun 26 '17 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

netusco
  • 101
  • 6