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