【问题标题】:How to apply policies to errors?如何将策略应用于错误?
【发布时间】:2014-02-18 16:13:18
【问题描述】:

我有一个名为“user”的控制器和一个名为“signup”的操作,然后我有一个名为“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,甚至不会运行我在 config/policies.js 中设置的任何策略

所以,我的问题是如何将我的自定义策略应用于包括错误页面(即 404)在内的所有内容

提前致谢!


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'],
  },


}

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    策略必须与要应用的策略匹配的路由。如果您想将此规则应用于所有 api 端点,那么您可以为此使用明确的自定义中间件。在此处查看堆栈溢出答案:Modify response header with sails.js for implementing HSTS

    【讨论】:

    • 非常感谢!浏览了您的答案,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2017-11-26
    相关资源
    最近更新 更多