method-override方法重写

NPM Version NPM Downloads Build Status Test Coverage

Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.允许您在客户端不支持的地方使用HTTP谓词,如PUT或DELETE。

Install安装

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

API

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).在需要了解请求方法的任何模块之前使用此模块是非常重要的(例如,它必须在csurf模块之前使用)。

methodOverride(getter, options)

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中提取。

If the found method is supported by node.js core, then req.method will be set to this value, as if it has originally been that value. 如果node.js core支持找到的方法,那么req.method将设置为该值,就像它最初是该值一样。The previous req.method value will be stored in req.originalMethod.先前的req.method值将存储在req.originalMethod中。

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. 如果提供了一个函数,则req作为第一个参数传递,res作为第二个参数传递,并且该方法将被返回。If a string is provided, the string is used to look up the method with the following rules:如果提供了字符串,则该字符串用于使用以下规则查找方法:

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 the override should arrive in. 这默认为仅POST方法,这是重写应该到达的唯一方法。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以允许所有方法。

Examples例子

override using a header使用标头重写

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 POST request to a URL with the overridden method as the value of that header. 然后,要进行调用,请向URL发送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)
}

override using a query value使用查询值重写

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 POST request to a URL with the overridden method as the value of that query string key. 然后,若要进行调用,请向URL发送POST请求,并将覆盖的方法作为该查询字符串键的值。This method of using a query value would typically be used in conjunction with plain HTML <form> elements when trying to support legacy browsers but still use newer methods.这种使用查询值的方法通常与普通HTML结合使用<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>

multiple format support多格式支持

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

custom logic自定义逻辑

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 <form>:使用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>

License许可证

MIT