代码之家  ›  专栏  ›  技术社区  ›  Jabaa

帆。js:删除特定路由的bodyparser中间件

  •  2
  • Jabaa  · 技术社区  · 9 年前

    有没有办法删除特定路由的中间件

    http.js 文件

    [
          'startRequestTimer',
          'cookieParser',
          'session',
          'bodyParser',
          'passportInit',
          'passportSession',
          'myRequestLogger',
          'handleBodyParserError',
          'compress',
          'methodOverride',
          'poweredBy',
          'router',
          'www',
          'favicon',
          '404',
          '500'
      ]
    

    bodyParser 特定路由的中间件

    有帆可以吗??

    1 回复  |  直到 9 年前
        1
  •  3
  •   sgress454    9 年前

    没有以声明方式执行此操作的机制,但可以在每个路由的基础上禁用中间件。您可以通过覆盖中间件并检查自定义代码中的路由URL来实现这一点,类似于 using a separate body parser for XML requests .例如:

    // In config/http.js `middleware` property
    
    bodyParser: (function() {
      // Initialize a skipper instance with the default options.
      var skipper = require('skipper')();
      // Create and return the middleware function.
      return function(req, res, next) {
        // If we see the route we want skipped, just continue.
        if (req.url === '/dont-parse-me') {
          return next();
        }
        // Otherwise use Skipper to parse the body.
        return skipper(req, res, next);
      };
    })()