Node.js 角度覆盖HttpErrorResponse对象属性

Node.js 角度覆盖HttpErrorResponse对象属性,node.js,angular,response,http-error,Node.js,Angular,Response,Http Error,我对Angular(8)相当陌生,但一直在构建一个前端SPA,它使用HttpClient模块对Node Express后端API进行http调用。在API响应中,我可以使用以下作为基本示例来设置Http响应状态和附加JSON对象: router.delete('/api/admin/roles', auth, (req, res) => { queries.deleteRole(req.query.roleid, (err,result) => { if (err) {

我对Angular(8)相当陌生,但一直在构建一个前端SPA,它使用HttpClient模块对Node Express后端API进行http调用。在API响应中,我可以使用以下作为基本示例来设置Http响应状态和附加JSON对象:

router.delete('/api/admin/roles', auth, (req, res) => {
  queries.deleteRole(req.query.roleid, (err,result) => {
    if (err) {
      if (err.number == 547) {
        return res.status(409).json({
          message: 'Users exist with this role. Cannot cascade delete.'
        })
      }
      return res.status(500).send()
    }
    return res.send(result)
  })
})
当我在开发环境中运行上述操作时,我可以成功截获HttpErrorResponse并使用message属性访问err.error对象。我的拦截器如下所示:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError((err: any) => {
        if (err instanceof HttpErrorResponse) {
          if (err.status === 401) {
            this.toastService.show(new Toast('Your session has expired and you have been logged out. Please log back in.', 'Session expired', {'delay': 5000, 'classname': 'bg-info text-light'}))
            this.authService.logOut();
          } else if (err.status === 409) {
            console.log(err)
            let errText = err.error.message || 'Record has been updated by another user. Please refresh page and retry.'
            this.toastService.show(new Toast(errText
            , 'Update conflict', {'delay': 5000, 'classname': 'bg-danger text-light'}))
          } else if (err.status == 403) {
            this.toastService.show(new Toast('You do not have the appropriate permissions to perform this action. Please contact your line manager to request access.'
            , 'Not authorized', {'delay': 5000, 'classname': 'bg-danger text-light'}))
          }
        }

        return next.handle(req)
      })
    )
  }
intercept(req:HttpRequest,next:HttpHandler):可观察{
返回next.handle(req.pipe)(
catchError((err:any)=>{
if(HttpErrorResponse的错误实例){
如果(错误状态===401){
this.toastService.show(新Toast('您的会话已过期,您已注销。请重新登录','会话已过期',{'delay':5000,'classname':'bg info text light'))
this.authService.logOut();
}else if(err.status==409){
console.log(错误)
让errText=err.error.message | |'记录已由其他用户更新。请刷新页面并重试。'
this.toastService.show(新Toast(errText
,'更新冲突',{'delay':5000,'classname':'bg danger text light'})
}else if(err.status==403){
this.toastService.show(new Toast)('您没有执行此操作的适当权限。请与您的直线经理联系以请求访问权限。'
,'未授权',{'delay':5000,'classname':'bg danger text light'})
}
}
返回下一个句柄(req)
})
)
}
这一切在开发中都很好。 但是,当我运行
ng build--prod
并将我的应用程序部署到IIS服务器(使用iisnode),并创建从API接收HttpError的相同条件时,我的response error属性不再包含我的自定义对象,而是恢复为“页面未显示,因为存在冲突。”,我猜这是给定409响应的标准字符串

我不明白为什么在我的Angular代码或节点中,它的行为会因环境而异,所以我假设这与
ng build--prod
命令或iisnode部署有关。谷歌并没有提供帮助,事实上,在我遇到的每一个留言板或教程上,建议的解决方案似乎都与我正在做的事情大体相符,没有迹象表明在部署到生产环境时可能会有任何差异,也没有任何人提到他们遇到了相同的问题


那么,我遗漏了什么明显的还是不明显的?如果有任何帮助,我们将不胜感激。

好的,所以我最终弄明白了这一点,一个包含“iisnode”一词的快速谷歌立即提供了一个解决方案。我不确定我以前怎么没有想到这一点

不管怎样,对于其他与此抗争的人,这是我为解决问题而遵循的链接:

基本上,您需要在iisnode上的web.config文件中添加两个内容:

  <configuration>
    <system.web>
      <customErrors mode="off" />    <-- this one here
    </system.web>
    <system.webServer>
      <httpErrors existingResponse="PassThrough" />    <-- and this one too
...