1
2/*!
3 * Connect - methodOverride
4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
6 * MIT Licensed
7 */
8
9/**
10 * Provides faux HTTP method support.
11 *
12 * Pass an optional `key` to use when checking for
13 * a method override, othewise defaults to _\_method_.
14 * The original method is available via `req.originalMethod`.
15 *
16 * @param {String} key
17 * @return {Function}
18 * @api public
19 */
20
21module.exports = function methodOverride(key){
22 key = key || "_method";
23 return function methodOverride(req, res, next) {
24 req.originalMethod = req.originalMethod || req.method;
25
26 // req.body
27 if (req.body && key in req.body) {
28 req.method = req.body[key].toUpperCase();
29 delete req.body[key];
30 // check X-HTTP-Method-Override
31 } else if (req.headers['x-http-method-override']) {
32 req.method = req.headers['x-http-method-override'].toUpperCase();
33 }
34
35 next();
36 };
37};
38