2017-06-30 24 views
1

有什麼辦法去除中間件特定路線Sails.js:刪除bodyparser中間件特定路線

目前所有的中間件在http.js文件中列出

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

我想刪除bodyParser中間件的特定路線

是否有可能與sails.js?

回答

2

沒有任何機制可以用聲明來做這件事,但是可以的,您可以在每個路由的基礎上禁用中間件。您可以通過覆蓋中間件並在自定義代碼中檢查路由URL來實現,類似於using a separate body parser for XML requests的解決方案。例如:

// 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。

+0

所以,我需要調用bodyParser中間件的順序排列 – Jabaa

+0

它已經在默認情況下。如果你有一個自定義的'訂單'數組,那麼是的,你需要確保'bodyParser'在那裏才能使這個解決方案起作用。 – sgress454

+0

所以我在這裏覆蓋默認的bodyParser中間件我是嗎? – Jabaa