dox-foundation-demo node_modules/express/node_modules/connect/lib/middleware/methodOverride.js

methods

declaration
methods

    Description

    Module dependencies.

    Source

    var methods = require('methods');

    exports

    method
    module.exports()
    • @param: {String}key
    • @return: {Function}

    Description

    Method Override:

    Provides faux HTTP method support.

    Pass an optional key to use when checking for
    a method override, othewise defaults to _method.
    The original method is available via req.originalMethod.

    Source

    module.exports = function methodOverride(key){
      key = key || "_method";
      return function methodOverride(req, res, next) {
        var method;
        req.originalMethod = req.originalMethod || req.method;
    
        // req.body
        if (req.body && key in req.body) {
          method = req.body[key].toLowerCase();
          delete req.body[key];
        }
    
        // check X-HTTP-Method-Override
        if (req.headers['x-http-method-override']) {
          method = req.headers['x-http-method-override'].toLowerCase();
        }
    
        // replace
        if (supports(method)) req.method = method.toUpperCase();
    
        next();
      };
    };

    supports

    function
    supports()

      Description

      Check if node supports method.

      Source

      function supports(method) {
        return ~methods.indexOf(method);
      }