Javascript Express.js抛出异常并返回预定义的状态代码

Javascript Express.js抛出异常并返回预定义的状态代码,javascript,node.js,express,exception,Javascript,Node.js,Express,Exception,在spring中,我使用define我自己的类扩展Exception用@ResponseStatus注释它,在特定情况下抛出它,以返回用户特定的状态代码和错误消息。我想在Node.js中的Express.js中做类似的事情。可能吗 现在我使用Q从loginService返回拒绝的承诺,并显式返回响应代码和消息 router.post('/log-in', jsonParser, (req, res) => { loginService.authenticate(req.body.use

在spring中,我使用define我自己的类扩展
Exception
@ResponseStatus
注释它,在特定情况下抛出它,以返回用户特定的状态代码和错误消息。我想在
Node.js
中的
Express.js
中做类似的事情。可能吗

现在我使用
Q
loginService
返回拒绝的承诺,并显式返回响应代码和消息

router.post('/log-in', jsonParser, (req, res) => {
  loginService.authenticate(req.body.userLogin, req.body.userPassword)
    .then((successLogin)=>{
    res.json(successLogin);
    }, function (error) {
      res.status(error.code).json({message: error.reason});
    })
});
但我的目标是只处理已解决的承诺,并在出现错误时抛出。差不多

router.post('/log-in', jsonParser, (req, res) => {
  loginService.authenticate(req.body.userLogin, req.body.userPassword)
    .then((successLogin)=>{
    res.json(successLogin);
    })
});
我想象服务投掷是这样的

if(!user) new Error(404, 'Cannot find user')
返回给用户的响应将包含所需的代码和消息。是否可以在Node中实现它?这样会有效率吗