Sails.js :remove bodyparser middleware for a specific route

巧了我就是萌 提交于 2019-12-12 10:15:07

问题


Is there any way to remove middleware for a particular route

currently all the middlewares are listed in http.js file

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

I want to remove the bodyParser middleware for a particular route

Is it possible with sails.js??


回答1:


There's no mechanism for doing this declaratively, but yes, you can disable a middleware on a per-route basis. You would do it by overriding the middleware and checking the route URL within your custom code, similar to the solution for using a separate body parser for XML requests. For example:

// 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);
  };
})()

That's the basic idea. It could certainly be done a bit more elegantly; for example if you had several routes you wanted skipped, you could save the list in a separate file and check the current URL against that list.



来源:https://stackoverflow.com/questions/44838905/sails-js-remove-bodyparser-middleware-for-a-specific-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!