Sails.js :删除特定路由的 bodyparser 中间件

Sails.js :remove bodyparser middleware for a specific route

有什么方法可以删除特定路由的中间件

目前所有中间件都在http.js文件中列出

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

我想删除特定路由的 bodyParser 中间件

sails.js可以吗??

没有以声明方式执行此操作的机制,但是可以,您可以在每个路由的基础上禁用中间件。您可以通过覆盖中间件并检查自定义代码中的路由 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);
  };
})()

这是基本思想。它当然可以做得更优雅一点;例如,如果你有几条你想跳过的路线,你可以将列表保存在一个单独的文件中,并根据该列表检查当前 URL。