问题
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