HTTP Method Override Middleware

3.0.0 · active · verified Wed Apr 22

method-override is a Node.js middleware designed for Express.js and similar frameworks, enabling clients to utilize HTTP verbs like PUT or DELETE even when their environment (e.g., older browsers, specific `XMLHttpRequest` implementations) primarily supports GET and POST. It achieves this by inspecting a specified request header (e.g., `X-HTTP-Method-Override`), a query string parameter (e.g., `_method`), or a custom function's return value to override the `req.method` property. The current stable version is 3.0.0. The package has a stable, but not high-frequency, release cadence, primarily focusing on maintaining compatibility within the Express.js ecosystem, updating dependencies, and adjusting Node.js version support. Its key differentiator lies in its flexibility in defining the 'getter' for the overridden method and its explicit warning about middleware order for security and functionality, positioning it as a robust solution for method simulation in web applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up `method-override` middleware in an Express application to allow HTTP method overriding via a custom header or a query string parameter, showing how to send requests that utilize these overrides via an HTML form and curl examples.

import express from 'express';
import methodOverride from 'method-override';

const app = express();
const port = 3000;

// --- Configuration of method-override middleware ---
// Option 1: Override using the 'X-HTTP-Method-Override' header
app.use(methodOverride('X-HTTP-Method-Override'));

// Option 2: Override using a query string parameter named '_method'
// This allows clients to send POST requests with ?_method=PUT or ?_method=DELETE
app.use(methodOverride('_method'));

// Option 3: Custom getter function (e.g., from a form field)
app.use(methodOverride(function (req, res) {
  if (req.body && typeof req.body === 'object' && '_method' in req.body) {
    // look in urlencoded POST bodies and delete it
    const method = req.body._method;
    delete req.body._method;
    return method;
  }
}));

// Ensure method-override is used before any routes or other middleware
// that rely on the correct HTTP method (e.g., csurf, route handlers).

// --- Example Routes ---
app.get('/', (req, res) => {
  res.send(`
    <h1>Method Override Example</h1>
    <p>Original Method: ${req.originalMethod}</p>
    <p>Current Method: ${req.method}</p>
    <form action="/resource?_method=DELETE" method="POST">
      <button type="submit">Delete via Query String</button>
    </form>
    <p>Try sending a POST request with 'X-HTTP-Method-Override: PUT' header to /resource.</p>
  `);
});

app.all('/resource', (req, res) => {
  const message = `Received a ${req.method} request to /resource (original: ${req.originalMethod || req.method})`;
  console.log(message);
  res.status(200).send(message);
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
  console.log('Test with curl:');
  console.log('  curl -X POST -H "X-HTTP-Method-Override: PUT" http://localhost:3000/resource');
  console.log('  curl -X POST "http://localhost:3000/resource?_method=DELETE"');
});

view raw JSON →