What is @types/method-override?
@types/method-override provides TypeScript definitions for the method-override middleware, which allows you to use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
What are @types/method-override's main functionalities?
Override HTTP Methods
This feature allows you to override HTTP methods using a custom header. In this example, the middleware checks for the 'X-HTTP-Method-Override' header and uses its value to override the HTTP method.
const methodOverride = require('method-override');
const express = require('express');
const app = express();
// Override with the X-HTTP-Method-Override header in the request
app.use(methodOverride('X-HTTP-Method-Override'));
app.post('/resource', (req, res) => {
res.send('POST request to the resource');
});
app.put('/resource', (req, res) => {
res.send('PUT request to the resource');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Override using a query value
This feature allows you to override HTTP methods using a query parameter. In this example, the middleware checks for the '_method' query parameter and uses its value to override the HTTP method.
const methodOverride = require('method-override');
const express = require('express');
const app = express();
// Override with the _method query parameter in the request
app.use(methodOverride('_method'));
app.post('/resource', (req, res) => {
res.send('POST request to the resource');
});
app.put('/resource', (req, res) => {
res.send('PUT request to the resource');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Other packages similar to @types/method-override
connect
Connect is a middleware framework for Node.js, offering a collection of high-performance 'plugins' known as middleware. It can be used to create a custom method override middleware similar to method-override.
body-parser
Body-parser is a middleware to handle the request body in Node.js. While it doesn't directly provide method overriding, it is often used in conjunction with method-override to parse incoming request bodies before method overriding.
express
Express is a web application framework for Node.js. It provides a robust set of features for web and mobile applications, including routing and middleware support. Method-override is often used as middleware in Express applications.
Installation
npm install --save @types/method-override
Summary
This package contains type definitions for method-override (https://github.com/expressjs/method-override).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/method-override.
declare namespace Express {
export interface Request {
originalMethod?: string | undefined;
}
}
import express = require("express");
declare namespace e {
export interface MethodOverrideOptions {
methods: string[];
}
}
declare function e(
getter?: string | ((req: express.Request, res: express.Response) => string),
options?: e.MethodOverrideOptions,
): express.RequestHandler;
export = e;
Additional Details
- Last updated: Wed, 18 Oct 2023 05:47:08 GMT
- Dependencies: @types/express
Credits
These definitions were written by Santi Albo.