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.
npm install method-overrideVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Node.js environment to a currently supported LTS version (e.g., Node.js 16 or newer) to ensure compatibility and receive security updates.
Always place `app.use(methodOverride(...))` at the beginning of your middleware chain, typically right after `express.json()` and `express.urlencoded()` if they are used, but before session management, CSRF protection, or routing definitions.
Avoid setting `options.methods` to `null` or including methods other than `POST` unless you fully understand the security implications and have mitigated potential risks. For most use cases, the default `['POST']` is appropriate and safer.
Ensure you call the `methodOverride` function with its required `getter` argument (e.g., a string for a header/query param or a custom function) to get the actual middleware function to pass to `app.use`.
For ESM projects, use `import methodOverride from 'method-override';`. If you must use `require` in an ESM file, consider dynamic import `const methodOverride = await import('method-override');` or ensure your environment supports CommonJS `require` calls.1. Verify `method-override` is at the very top of your `app.use` calls. 2. Double-check the exact header name or query parameter key used in the client request matches the `getter` argument. 3. Ensure the original request from the client is a `POST` request, as this is the default and safest method for overrides.