Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.允许您在客户端不支持的地方使用HTTP谓词,如PUT或DELETE。
This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表呀获得。Installation is done using the 使用npm install
command:npm install
命令完成安装:
$ 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模块之前使用)。csurf
module).
Create a new middleware function to override the 创建一个新的中间件函数,用一个新值覆盖req.method
property with a new value. req.method
属性。This value will be pulled from the provided 此值将从提供的getter
.getter
中提取。
getter
- getter
。X-HTTP-Method-Override
)X-HTTP-Method-Override
)options.methods
- ['POST']
)['POST']
)If the found method is supported by node.js core, then 如果node.js core支持找到的方法,那么req.method
will be set to this value, as if it has originally been that value. req.method
将设置为该值,就像它最初是该值一样。The previous 先前的req.method
value will be stored in req.originalMethod
.req.method
值将存储在req.originalMethod
中。
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. req
作为第一个参数传递,res
作为第二个参数传递,并且该方法将被返回。If a string is provided, the string is used to look up the method with the following rules:如果提供了字符串,则该字符串用于使用以下规则查找方法:
X-
, then it is treated as the name of a header and that header is used for the method override. X-
开头,则将其视为标头的名称,并且该标头用于方法重写。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方法,这是重写应该到达的唯一方法。POST
methods, which is the only method the 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. 此值是大写的方法数组。可以指定null
can be specified to allow all methods.null
以允许所有方法。
To use a header to override the method, specify the header name as a string argument to the 要使用标头重写方法,请将标头名称指定为methodOverride
function. methodOverride
函数的字符串参数。To then make the call, send a 然后,要进行调用,请向URL发送POST
request to a URL with the overridden method as the value of that header. POST
请求,并将覆盖的方法作为该标头的值。This method of using a header would typically be used in conjunction with 对于不支持您尝试使用的方法的实现,这种使用头的方法通常会与XMLHttpRequest
on implementations that do not support the method you are trying to use.XMLHttpRequest
结合使用。
var express = require('express')
var methodOverride = require('method-override')
var app = express()
// override with the X-HTTP-Method-Override header in the request在请求中使用X-HTTP-Method-override头进行覆盖
app.use(methodOverride('X-HTTP-Method-Override'))
Example call with header override using 使用XMLHttpRequest
:XMLHttpRequest
进行头覆盖的调用示例:
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. methodOverride
函数的字符串参数。To then make the call, send a 然后,若要进行调用,请向URL发送POST请求,并将覆盖的方法作为该查询字符串键的值。POST
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结合使用<form>
elements when trying to support legacy browsers but still use newer methods.<form>
元素,但仍使用更新的方法。
var express = require('express')
var methodOverride = require('method-override')
var app = express()
// override with POST having ?_method=DELETE
app.use(methodOverride('_method'))
Example call with query override using HTML <form>
:
<form method="POST" action="/resource?_method=DELETE">
<button type="submit">Delete resource</button>
</form>
var express = require('express')
var methodOverride = require('method-override')
var app = express()
// override with different headers; last one takes precedence用不同的标题覆盖;最后一个优先
app.use(methodOverride('X-HTTP-Method')) // Microsoft
app.use(methodOverride('X-HTTP-Method-Override')) // Google/GData
app.use(methodOverride('X-Method-Override')) // IBM
You can implement any kind of custom logic with a function for the 您可以使用getter
. getter
函数实现任何类型的自定义逻辑。The following implements the logic for looking in 下面实现了查找req.body
that was in method-override@1
:method-override@1
中的req.body
的逻辑:
var bodyParser = require('body-parser')
var express = require('express')
var methodOverride = require('method-override')
var app = express()
// NOTE: when using req.body, you must fully parse the request body before you call methodOverride() in your middleware stack,使用req.body时,在调用中间件堆栈中的methodOverride()之前,必须完全解析请求体,
// otherwise req.body will not be populated.否则,将不填充req.body。
app.use(bodyParser.urlencoded())
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}))
Example call with query override using HTML 使用HTML<form>
:<form>
的查询覆盖调用示例:
<!-- enctype must be set to the type you will parse before methodOverride() -->
<form method="POST" action="/resource" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete resource</button>
</form>