Koa Method Override Middleware
raw JSON →koa-override is a Koa middleware designed to enable clients to utilize HTTP verbs like PUT or DELETE in environments where direct support is limited, typically by overriding the method via a `_method` field in the request body or an `X-HTTP-Method-Override` header. The current stable version is 4.0.0, released in June 2024. This package is maintained by the Egg.js team, indicating active development and stability within the Koa ecosystem. It provides a simple API, `override([options])`, allowing configuration of allowed override methods (defaulting to `POST` requests). A key differentiator is its explicit method overriding mechanism, making it a robust solution for RESTful API compatibility challenges with legacy clients or specific network constraints. It ships with TypeScript types and supports both CommonJS and ES Modules since version 4.0.0.
Common errors
error TypeError: override is not a function ↓
app.use(override) to app.use(override()). error Method override not working when using _method in form data ↓
koa-bodyparser, before koa-override. Example: app.use(bodyParser()); app.use(override()); error Method override not working for GET requests or other non-POST methods ↓
koa-override with the allowedMethods option to include the desired request types, e.g., app.use(override({ allowedMethods: ['POST', 'GET'] })); Warnings
breaking Version 4.0.0 drops support for Node.js versions older than 18.19.0. Projects on older Node.js versions must upgrade Node.js or remain on `koa-override` v3. ↓
gotcha To override methods using the `_method` field in the request body (e.g., from an HTML form), a body parsing middleware like `koa-bodyparser` is required to parse the request body before `koa-override` can access it. ↓
gotcha By default, `koa-override` only allows method overriding on `POST` requests. Attempts to override methods on `GET` or other request types will be ignored unless `options.allowedMethods` is configured. ↓
Install
npm install koa-override yarn add koa-override pnpm add koa-override Imports
- override wrong
import { override } from 'koa-override';correctimport override from 'koa-override'; - override (CommonJS)
const override = require('koa-override'); - Middleware usage wrong
app.use(override);correctapp.use(override());
Quickstart
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import override from 'koa-override';
const app = new Koa();
// Add a body parser to handle _method in the request body
app.use(bodyParser());
// Apply the method override middleware
// By default, it checks `body._method` then `X-HTTP-Method-Override` header
// Overrides are only allowed on POST requests by default.
app.use(override());
app.use(async (ctx, next) => {
if (ctx.method === 'DELETE') {
ctx.body = `Received a simulated DELETE request for ${ctx.url}`;
} else if (ctx.method === 'PUT') {
ctx.body = `Received a simulated PUT request for ${ctx.url} with body: ${JSON.stringify(ctx.request.body)}`;
} else {
await next();
}
});
app.use(async (ctx) => {
ctx.body = `Received original ${ctx.method} request for ${ctx.url}`;
});
const port = 3000;
app.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`);
console.log('Try POSTing to /test with X-HTTP-Method-Override: PUT, or with body: { "_method": "DELETE" }');
});