注意
此页面是根据 method-override README 生成的。method-override
允许你在客户端不支持 HTTP 动词(如 PUT 或 DELETE)的地方使用它们。
安装
这是一个通过 npm 注册表 提供的 Node.js 模块。使用 npm install
命令 进行安装。
$ npm install method-override
API
注意:此模块在任何需要知道请求方法的模块(例如,它必须在 csurf
模块之前使用)之前使用非常重要。
methodOverride(getter, options)
创建一个新的中间件函数,用新值覆盖 req.method
属性。此值将从提供的 getter
中获取。
getter
- 用于查找请求的被覆盖方法的获取器。(默认值:X-HTTP-Method-Override
)options.methods
- 原始请求必须包含的允许方法,以便检查方法覆盖值。(默认值:['POST']
)
如果找到的方法受 Node.js 核心支持,则 req.method
将被设置为此值,就好像它最初就是该值一样。之前的 req.method
值将存储在 req.originalMethod
中。
getter
这是从请求中获取覆盖值的方法。如果提供的是函数,req
将作为第一个参数传递,res
作为第二个参数传递,并期望返回该方法。如果提供的是字符串,则使用该字符串按照以下规则查找方法:
- 如果字符串以
X-
开头,则将其视为标头名称,并使用该标头进行方法覆盖。如果请求包含相同标头多次,则使用第一次出现的值。 - 所有其他字符串都视为 URL 查询字符串中的一个键。
options.methods
这允许指定请求必须是哪些方法才能检查方法覆盖值。默认仅限于 POST
方法,这是覆盖值应到达的唯一方法。可以在此处指定更多方法,但这可能会引入安全问题,并在请求通过缓存时导致奇怪的行为。此值是一个大写方法的数组。null
可用于允许所有方法。
示例
使用标头覆盖
要使用标头覆盖方法,请将标头名称指定为 methodOverride
函数的字符串参数。然后,发送一个 POST
请求到 URL,并将覆盖的方法作为该标头的值。这种使用标头的方法通常与 XMLHttpRequest
结合使用,适用于不支持你尝试使用的方法的实现。
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'))
使用 XMLHttpRequest
进行标头覆盖的调用示例
const 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)
}
使用查询值覆盖
要使用查询字符串值覆盖方法,请将查询字符串键指定为 methodOverride
函数的字符串参数。然后,发送一个 POST
请求到 URL,并将覆盖的方法作为该查询字符串键的值。这种使用查询值的方法通常与普通 HTML <form>
元素结合使用,以支持旧版浏览器但仍使用较新方法。
const express = require('express')
const methodOverride = require('method-override')
const app = express()
// override with POST having ?_method=DELETE
app.use(methodOverride('_method'))
使用 HTML <form>
进行查询覆盖的调用示例
<form method="POST" action="/resource?_method=DELETE">
<button type="submit">Delete resource</button>
</form>
多格式支持
const express = require('express')
const methodOverride = require('method-override')
const 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
自定义逻辑
你可以使用函数为 getter
实现任何类型的自定义逻辑。以下实现了 method-override@1
中用于查找 req.body
的逻辑。
const bodyParser = require('body-parser')
const express = require('express')
const methodOverride = require('method-override')
const app = express()
// NOTE: when using req.body, you must fully parse the request body
// before you call methodOverride() in your middleware stack,
// otherwise req.body will not be populated.
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
const method = req.body._method
delete req.body._method
return method
}
}))
使用 HTML <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>