Sails.js :remove bodyparser middleware for a specific route

好久不见. 提交于 2019-12-07 09:59:34

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.

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