Sails.js : 删除特定路由的 bodyparser 中间件 [英] Sails.js :remove bodyparser middleware for a specific route
问题描述
有没有办法删除特定路由的中间件
目前所有的中间件都列在 http.js
文件中
我想删除特定路由的 bodyParser
中间件
sails.js 可以吗??
没有以声明方式执行此操作的机制,但是可以,您可以在每个路由的基础上禁用中间件.您可以通过覆盖中间件并检查自定义代码中的路由 URL 来实现,类似于 对 XML 请求使用单独的正文解析器.例如:
//在 config/http.js `middleware` 属性中bodyParser: (function() {//使用默认选项初始化一个船长实例.var skipper = require('skipper')();//创建并返回中间件函数.返回函数(请求,资源,下一个){//如果我们看到想要跳过的路线,就继续.if (req.url === '/dont-parse-me') {返回下一个();}//否则使用 Skipper 解析正文.返回船长(请求,资源,下一个);};})()
这是基本的想法.它当然可以做得更优雅一些;例如,如果您想跳过几条路线,您可以将列表保存在一个单独的文件中,然后根据该列表检查当前 URL.
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??
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.
这篇关于Sails.js : 删除特定路由的 bodyparser 中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!