Override HTTP verbs
npm install method-override[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
This is a Node.js module available through the
npm registry. Installation is done using thenpm install command:
``sh`
$ npm install method-override
NOTE It is very important that this module is used before any module that
needs to know the method of the request (for example, it _must_ be used prior to
the csurf module).
Create a new middleware function to override the req.method property with a newgetter
value. This value will be pulled from the provided .
- getter - The getter to use to look up the overridden request method for the request. (default: X-HTTP-Method-Override)options.methods
- - The allowed methods the original request must be in to check for a method override value. (default: ['POST'])
If the found method is supported by node.js core, then req.method will be set toreq.method
this value, as if it has originally been that value. The previous req.originalMethod
value will be stored in .
#### getter
This is the method of getting the override value from the request. If a function is provided,
the req is passed as the first argument, the res as the second argument and the method is
expected to be returned. If a string is provided, the string is used to look up the method
with the following rules:
- If the string starts with X-, then it is treated as the name of a header and that header
is used for the method override. If the request contains the same header multiple times, the
first occurrence is used.
- All other strings are treated as a key in the URL query string.
#### options.methods
This allows the specification of what methods(s) the request MUST be in in order to check for
the method override value. This defaults to only POST methods, which is the only method thenull
override should arrive in. More methods may be specified here, but it may introduce security
issues and cause weird behavior when requests travel through caches. This value is an array
of methods in upper-case. can be specified to allow all methods.
To use a header to override the method, specify the header name
as a string argument to the methodOverride function. To then makePOST
the call, send a request to a URL with the overridden methodXMLHttpRequest
as the value of that header. This method of using a header would
typically be used in conjunction with on implementations
that do not support the method you are trying to use.
`js
var express = require('express')
var methodOverride = require('method-override')
var app = express()
// override with the X-HTTP-Method-Override header in the request
app.use(methodOverride('X-HTTP-Method-Override'))
`
Example call with header override using XMLHttpRequest:
`js
var xhr = new XMLHttpRequest()
xhr.onload = onload
xhr.open('post', '/resource', true)
xhr.setRequestHeader('X-HTTP-Method-Override', 'DELETE')
xhr.send()
function onload () {
alert('got response: ' + this.responseText)
}
`
To use a query string value to override the method, specify the query
string key as a string argument to the methodOverride function. ToPOST
then make the call, send a request to a URL with the overridden
method as the value of that query string key. This method of using a
query value would typically be used in conjunction with plain HTML
:`html
``[npm-image]: https://img.shields.io/npm/v/method-override.svg
[npm-url]: https://npmjs.org/package/method-override
[travis-image]: https://img.shields.io/travis/expressjs/method-override/master.svg
[travis-url]: https://travis-ci.org/expressjs/method-override
[coveralls-image]: https://img.shields.io/coveralls/expressjs/method-override/master.svg
[coveralls-url]: https://coveralls.io/r/expressjs/method-override?branch=master
[downloads-image]: https://img.shields.io/npm/dm/method-override.svg
[downloads-url]: https://npmjs.org/package/method-override