有没有办法删除特定路由的中间件
目前所有中间件都列在http.js
file
[
'startRequestTimer',
'cookieParser',
'session',
'bodyParser',
'passportInit',
'passportSession',
'myRequestLogger',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'router',
'www',
'favicon',
'404',
'500'
]
我想删除bodyParser
特定路由的中间件
sails.js 可以吗?
没有以声明方式执行此操作的机制,但是,您可以在每个路由的基础上禁用中间件。您可以通过覆盖中间件并检查自定义代码中的路由 URL 来完成此操作,类似于以下解决方案对 XML 请求使用单独的正文解析器。例如:
// 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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)