2014-02-18 37 views
1

我有一個名爲「用戶」和動作控制器稱爲「註冊」,然後我也被稱爲政策「的stripslashes」使用下面的代碼:如何將策略應用於錯誤?

module.exports = function(req, res, next) { 
    if(req.url.substr(-1) === '/' && req.url.length > 1){ 
    res.redirect(req.url.slice(0, -1), 301); 
    } 
    else { 
    next(); 
    } 
}; 

我所試圖做的是每當http://localhost:{{port}}/user/signup/被訪問它將剝離斜線並重定向到:http://localhost:{{port}}/user/signup這是完美的作品!

除此之外,每當我嘗試並參觀http://localhost:{{port}}/user/signup/////例如它只是拋出一個404甚至不通過任何我在配置設置政策的運行/ policies.js

所以,我的問題是如何我是否將自定義策略應用於一切,包括錯誤頁面(即404s)

感謝高級!


Policies.js供參考:

module.exports.policies = { 

    '*': ['flash', 'stripSlashes', 'wwwRedirect'], 

    user: { 
    edit: ['flash', 'isAuthenticated', 'stripSlashes', 'wwwRedirect'], 
    update: ['flash', 'isAuthenticated', 'stripSlashes', 'wwwRedirect'], 
    show: ['flash', 'isAuthenticated', 'stripSlashes', 'wwwRedirect'], 
    index: ['flash', 'isAuthenticated', 'stripSlashes', 'wwwRedirect'], 
    destroy: ['flash', 'isAuthenticated', 'stripSlashes', 'wwwRedirect'], 
    }, 


} 

回答

1

策略具有用於要應用的策略,以匹配的路線。如果您想將此規則應用於所有api端點,那麼您可以爲此使用快速自定義中間件。請參閱堆棧溢出答案:Modify response header with sails.js for implementing HSTS

+0

謝謝!通過你的答案,它完美的工作。 –