Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Connect.js methodOverride do?

People also ask

How do I use methodOverride?

To use a header to override the method, specify the header name as a string argument to the methodOverride function. To then make the call, send a POST request to a URL with the overridden method as the value of that header.

What is methodOverride in node JS?

The methodOverride() middleware is for requests from clients that only natively support simple verbs like GET and POST. So in those cases you could specify a special query field (or a hidden form field for example) that indicates the real verb to use instead of what was originally sent.

What is Connect middleware?

What is connect? From the README: Connect is an extensible HTTP server framework for node, providing high performance "plugins" known as middleware. More specifically, connect wraps the Server, ServerRequest, and ServerResponse objects of node.

Why We Use Connect in Javascript?

Connect also offers a createServer method, which returns an object that inherits an extended version of http. Server . Connect's extensions are mainly there to make it easy to plug in middleware. That's why Connect describes itself as a "middleware framework," and is often analogized to Ruby's Rack.


  • If you want to simulate DELETE and PUT, methodOverride is for that.
  • If you pass in the _method post parameter set to 'delete' or 'put', then you can use app.delete and app.put in Express instead of using app.post all the time (thus more descriptive, verbose):

Backend:

// the app
app.put('/users/:id', function (req, res, next) {
  // edit your user here
});

Client logic:

// client side must be..
<form> ...
  <input type="hidden" name="_method" value="put" />
</form>