You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
940 B

  1. /*!
  2. * Connect - methodOverride
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. /**
  8. * Method Override:
  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. module.exports = function methodOverride(key){
  21. key = key || "_method";
  22. return function methodOverride(req, res, next) {
  23. req.originalMethod = req.originalMethod || req.method;
  24. // req.body
  25. if (req.body && key in req.body) {
  26. req.method = req.body[key].toUpperCase();
  27. delete req.body[key];
  28. // check X-HTTP-Method-Override
  29. } else if (req.headers['x-http-method-override']) {
  30. req.method = req.headers['x-http-method-override'].toUpperCase();
  31. }
  32. next();
  33. };
  34. };