Sails Hook路线被禁止

时间:2015-03-28 12:46:36

标签: node.js express routes hook sails.js

我尝试创建一个可安装的钩子来进行一些安全性验证,所以在我的hook.js下我设置:

routes     : {
      before : {
        "/" : function (req, res, view)
        {
          .....
          res.forbidden();
        }
      }
    },

如果我尝试向用户发送禁止页面,则会出现此错误:

error: Sending 500 ("Server Error") response: 
 TypeError: Object #<ServerResponse> has no method 'view'
    at Object.forbidden (/Users/jaumard/Documents/workspaceIDE/KikiLib/api/responses/forbidden.js:56:19)
    at ServerResponse.bound [as forbidden] (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
    at isRouteAllowed (/Users/jaumard/Documents/workspaceIDE/KikiLib/api/hooks/acl.js:66:18)
    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /usr/local/lib/node_modules/sails/lib/router/bind.js:187:7
    at /usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:35:7
    at Object.i18nInit [as init] (/usr/local/lib/node_modules/sails/node_modules/i18n/i18n.js:89:5)
    at addLocalizationMethod (/usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:33:11)
    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11) [TypeError: Object #<ServerResponse> has no method 'view']
error: Server Error:
error: TypeError: Object #<ServerResponse> has no method 'view'
    at Object.forbidden (/Users/jaumard/Documents/workspaceIDE/KikiLib/api/responses/forbidden.js:56:19)
    at ServerResponse.bound [as forbidden] (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
    at isRouteAllowed (/Users/jaumard/Documents/workspaceIDE/KikiLib/api/hooks/acl.js:66:18)
    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
    at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
    at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
    at /usr/local/lib/node_modules/sails/lib/router/bind.js:187:7
    at /usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:35:7
    at Object.i18nInit [as init] (/usr/local/lib/node_modules/sails/node_modules/i18n/i18n.js:89:5)
    at addLocalizationMethod (/usr/local/lib/node_modules/sails/lib/hooks/i18n/index.js:33:11)
    at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
    at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
    at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11) [TypeError: Object #<ServerResponse> has no method 'view']

我在风帆0.11.0下。我没有使用策略,因为策略仅适用于控制器操作,我想为视图,控制器等创建ACL ...

此处提供的所有源代码均为https://github.com/jaumard/sails-hook-user-acl

现在我使用:res.status(403).send("<h1>" + req.__("Forbidden") + "</h1>");代替res.forbidden();,但发送视图会很不错

1 个答案:

答案 0 :(得分:1)

刚刚找到了解决方法:

var _addResViewMethod = require("../sails/lib/hooks/views/res.view.js");
routes     : {
      before : {
        "/office" : function (req, res, view)
        {
          .....
          _addResViewMethod(req, res, function(){
               res.forbidden();
          });
        }
      }
    },

编辑:帆船队在https://github.com/balderdashy/sails/issues/2799#issuecomment-115333582

给出了一个更好的方法
// Wait for the router to be initialized     
sails.on('router:before', function () {

    // Wait for the views hook to load, so that its routes get bound first 
    // (including the one that mixes in res.view)
    sails.after('hook:views:loaded', function () {
      // Bind your routes here
      sails.router.bind('/office', officeHandler, 'get', {});
    });

    function officeHandler(req, res) {...res.forbidden()...}

    return cb();
});