【问题标题】:Unable to change error response code in feathers.js无法更改 feathers.js 中的错误响应代码
【发布时间】:2017-11-07 05:18:14
【问题描述】:

我正在使用身份验证服务进行登录验证。因为我想未经授权 (401) 响应代码为 200,并且消息应该相同。

我的身份验证服务是

app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),
        function (hook) {
         hook.params.payload = {
            userId: hook.params.user.userId,
            accountId: hook.params.user.accountId
          };
          return Promise.resolve(hook);
        }
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    },
    after: {
      create: [ function(hook,next){
          hook.result.statusCode = 201; 
          hook.result.authentication = "user login successful";
          next();
        }
      ]
    }
  });

我的中间件代码是

app.use(function(err, req, res, next) {
  res.status(err.status || 200);

  res.format({
    'text/html': function(){
      // Probably render a nice error page here
      return res.send(err);
    },

    'application/json': function(){
      res.json(err);
    },

    'text/plain': function(){
      res.send(err.message);
    }
  });
});

我的回复信息是

{
    "name": "NotAuthenticated",
    "message": "Invalid login",
    "code": 401,
    "className": "not-authenticated",
    "data": {
        "message": "Invalid login"
    },
    "errors": {}
}

但我想要

{
    "name": "NotAuthenticated",
    "message": "Invalid login",
    "code": 200,
    "className": "not-authenticated",
    "data": {
        "message": "Invalid login"
    },
    "errors": {}
}

【问题讨论】:

  • res.status(err.status === 401 ? 200 : (err.status || 200)?
  • 它不工作@wbadart

标签: javascript node.js response middleware feathersjs


【解决方案1】:

终于找到了解决办法,我们需要在钩子错误方法中更改响应代码。

错误响应代码更改:

app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),
        function (hook) {
         hook.params.payload = {
            userId: hook.params.user.userId,
            accountId: hook.params.user.accountId
          };
          return Promise.resolve(hook);
        }
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    },
    after: {
      create: [ function(hook,next){
          hook.result.code = 200; 
          hook.result.authentication = "user login successful";
          next();
        }
      ]
    },
    error: {
      create: [function(hook, next){
        hook.error.code = 200;
        next();
      }]
    }
  });

结果响应代码更改:

function restFormatter(req, res) {
  res.format({
    'application/json': function() {
      const data = res.data;
      res.status(data.__status || 200);
      res.json(data);
    }
  });
}

app.configure(rest(restFormatter));

【讨论】:

    【解决方案2】:

    另一种选择是通过在 error 处理程序中设置 hook.result 来消除错误,这将自动返回成功的 HTTP 代码:

    app.service('authentication').hooks({
      error: {
        create: [ function(hook, next){
          hook.result = { authentication: "user login successful" };
        } ]
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      相关资源
      最近更新 更多