Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

method-override

Package Overview
Dependencies
Maintainers
6
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

method-override

Override HTTP verbs

  • 2.3.10
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created

What is method-override?

The method-override npm package allows you to use HTTP verbs such as PUT or DELETE in places where the client doesn't support it. This is particularly useful for working with forms in web applications, as HTML forms only support GET and POST methods.

What are method-override's main functionalities?

Override using a query value

This feature allows you to override the HTTP method using a query parameter. For example, a POST request to /resource?_method=PUT will be treated as a PUT request.

const express = require('express');
const methodOverride = require('method-override');
const app = express();

// override with the X-HTTP-Method-Override header 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');
});

Override using a header

This feature allows you to override the HTTP method using a custom header. For example, a POST request with the header X-HTTP-Method-Override: PUT will be treated as a PUT request.

const express = require('express');
const methodOverride = require('method-override');
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 function

This feature allows you to override the HTTP method using a custom function. The function can inspect the request and determine the method to override.

const express = require('express');
const methodOverride = require('method-override');
const app = express();

// override with a function
app.use(methodOverride((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;
  }
}));

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 method-override

FAQs

Package last updated on 27 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc