rc@73
|
1 # method-override
|
rc@73
|
2
|
rc@73
|
3 [](http://badge.fury.io/js/method-override)
|
rc@73
|
4 [](https://travis-ci.org/expressjs/method-override)
|
rc@73
|
5 [](https://coveralls.io/r/expressjs/method-override)
|
rc@73
|
6
|
rc@73
|
7 Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
|
rc@73
|
8
|
rc@73
|
9 ## Install
|
rc@73
|
10
|
rc@73
|
11 ```sh
|
rc@73
|
12 $ npm install method-override
|
rc@73
|
13 ```
|
rc@73
|
14
|
rc@73
|
15 ## API
|
rc@73
|
16
|
rc@73
|
17 **NOTE** It is very important that this module is used **before** any module that
|
rc@73
|
18 needs to know the method of the request (for example, it _must_ be used prior to
|
rc@73
|
19 the `csurf` module).
|
rc@73
|
20
|
rc@73
|
21 ### methodOverride(getter, options)
|
rc@73
|
22
|
rc@73
|
23 Create a new middleware function to override the `req.method` property with a new
|
rc@73
|
24 value. This value will be pulled from the provided `getter`.
|
rc@73
|
25
|
rc@73
|
26 - `getter` - The getter to use to look up the overridden request method for the request. (default: `X-HTTP-Method-Override`)
|
rc@73
|
27 - `options.methods` - The allowed methods the original request must be in to check for a method override value. (default: `['POST']`)
|
rc@73
|
28
|
rc@73
|
29 If the found method is supported by node.js core, then `req.method` will be set to
|
rc@73
|
30 this value, as if it has originally been that value. The previous `req.method`
|
rc@73
|
31 value will be stored in `req.originalMethod`.
|
rc@73
|
32
|
rc@73
|
33 #### getter
|
rc@73
|
34
|
rc@73
|
35 This is the method of getting the override value from the request. If a function is provided,
|
rc@73
|
36 the `req` is passed as the first argument, the `res as the second argument and the method is
|
rc@73
|
37 expected to be returned. If a string is provided, the string is used to look up the method
|
rc@73
|
38 with the following rules:
|
rc@73
|
39
|
rc@73
|
40 - If the string starts with `X-`, then it is treated as the name of a header and that header
|
rc@73
|
41 is used for the method override. If the request contains the same header multiple times, the
|
rc@73
|
42 first occurrence is used.
|
rc@73
|
43 - All other strings are treated as a key in the URL query string.
|
rc@73
|
44
|
rc@73
|
45 #### options.methods
|
rc@73
|
46
|
rc@73
|
47 This allows the specification of what methods(s) the request *MUST* be in in order to check for
|
rc@73
|
48 the method override value. This defaults to only `POST` methods, which is the only method the
|
rc@73
|
49 override should arrive in. More methods may be specified here, but it may introduce security
|
rc@73
|
50 issues and cause weird behavior when requests travel through caches. This value is an array
|
rc@73
|
51 of methods in upper-case. `null` can be specified to allow all methods.
|
rc@73
|
52
|
rc@73
|
53 ## Examples
|
rc@73
|
54
|
rc@73
|
55 ### override using a header
|
rc@73
|
56
|
rc@73
|
57 To use a header to override the method, specify the header name
|
rc@73
|
58 as a string argument to the `methodOverride` function. To then make
|
rc@73
|
59 the call, send a `POST` request to a URL with the overridden method
|
rc@73
|
60 as the value of that header.
|
rc@73
|
61
|
rc@73
|
62 ```js
|
rc@73
|
63 var connect = require('connect')
|
rc@73
|
64 var methodOverride = require('method-override')
|
rc@73
|
65
|
rc@73
|
66 // override with the X-HTTP-Method-Override header in the request
|
rc@73
|
67 app.use(methodOverride('X-HTTP-Method-Override'))
|
rc@73
|
68 ```
|
rc@73
|
69
|
rc@73
|
70 Example call with header override using `curl`:
|
rc@73
|
71
|
rc@73
|
72 ```
|
rc@73
|
73 curl -XPOST -H'X-HTTP-Method-Override: DELETE' --verbose http://localhost:3000/resource
|
rc@73
|
74 > POST /resource HTTP/1.1
|
rc@73
|
75 > Host: localhost:3000
|
rc@73
|
76 > X-HTTP-Method-Override: DELETE
|
rc@73
|
77 >
|
rc@73
|
78 Cannot DELETE /resource
|
rc@73
|
79 ```
|
rc@73
|
80
|
rc@73
|
81 ### override using a query value
|
rc@73
|
82
|
rc@73
|
83 To use a query string value to override the method, specify the query
|
rc@73
|
84 string key as a string argument to the `methodOverride` function. To
|
rc@73
|
85 then make the call, send a `POST` request to a URL with the overridden
|
rc@73
|
86 method as the value of that query string key.
|
rc@73
|
87
|
rc@73
|
88 ```js
|
rc@73
|
89 var connect = require('connect')
|
rc@73
|
90 var methodOverride = require('method-override')
|
rc@73
|
91
|
rc@73
|
92 // override with POST having ?_method=DELETE
|
rc@73
|
93 app.use(methodOverride('_method'))
|
rc@73
|
94 ```
|
rc@73
|
95
|
rc@73
|
96 Example call with query override using `curl`:
|
rc@73
|
97
|
rc@73
|
98 ```
|
rc@73
|
99 curl -XPOST --verbose http://localhost:3000/resource?_method=DELETE
|
rc@73
|
100 > POST /resource?_method=DELETE HTTP/1.1
|
rc@73
|
101 > Host: localhost:3000
|
rc@73
|
102 >
|
rc@73
|
103 Cannot DELETE /resource?_method=DELETE
|
rc@73
|
104 ```
|
rc@73
|
105
|
rc@73
|
106 ### multiple format support
|
rc@73
|
107
|
rc@73
|
108 ```js
|
rc@73
|
109 var connect = require('connect')
|
rc@73
|
110 var methodOverride = require('method-override')
|
rc@73
|
111
|
rc@73
|
112 // override with different headers; last one takes precedence
|
rc@73
|
113 app.use(methodOverride('X-HTTP-Method')) // Microsoft
|
rc@73
|
114 app.use(methodOverride('X-HTTP-Method-Override')) // Google/GData
|
rc@73
|
115 app.use(methodOverride('X-Method-Override')) // IBM
|
rc@73
|
116 ```
|
rc@73
|
117
|
rc@73
|
118 ### custom logic
|
rc@73
|
119
|
rc@73
|
120 You can implement any kind of custom logic with a function for the `getter`. The following
|
rc@73
|
121 implements the logic for looking in `req.body` that was in `method-override` 1:
|
rc@73
|
122
|
rc@73
|
123 ```js
|
rc@73
|
124 var bodyParser = require('body-parser')
|
rc@73
|
125 var connect = require('connect')
|
rc@73
|
126 var methodOverride = require('method-override')
|
rc@73
|
127
|
rc@73
|
128 app.use(bodyParser.urlencoded())
|
rc@73
|
129 app.use(methodOverride(function(req, res){
|
rc@73
|
130 if (req.body && typeof req.body === 'object' && '_method' in req.body) {
|
rc@73
|
131 // look in urlencoded POST bodies and delete it
|
rc@73
|
132 var method = req.body._method
|
rc@73
|
133 delete req.body._method
|
rc@73
|
134 return method
|
rc@73
|
135 }
|
rc@73
|
136 }))
|
rc@73
|
137 ```
|
rc@73
|
138
|
rc@73
|
139 ## License
|
rc@73
|
140
|
rc@73
|
141 The MIT License (MIT)
|
rc@73
|
142
|
rc@73
|
143 Copyright (c) 2014 Jonathan Ong me@jongleberry.com
|
rc@73
|
144
|
rc@73
|
145 Permission is hereby granted, free of charge, to any person obtaining a copy
|
rc@73
|
146 of this software and associated documentation files (the "Software"), to deal
|
rc@73
|
147 in the Software without restriction, including without limitation the rights
|
rc@73
|
148 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
rc@73
|
149 copies of the Software, and to permit persons to whom the Software is
|
rc@73
|
150 furnished to do so, subject to the following conditions:
|
rc@73
|
151
|
rc@73
|
152 The above copyright notice and this permission notice shall be included in
|
rc@73
|
153 all copies or substantial portions of the Software.
|
rc@73
|
154
|
rc@73
|
155 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
rc@73
|
156 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
rc@73
|
157 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
rc@73
|
158 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
rc@73
|
159 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
rc@73
|
160 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
rc@73
|
161 THE SOFTWARE.
|