【发布时间】: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