我在应用程序中有两个组件。基于Angular构建的前端和使用express构建的后端。我在后端使用Nest.js框架。
我有一个http-exception.filter.ts
文件,该文件可以处理所有引发的异常。到目前为止,我已经通过这种方式在应用程序中处理内部服务器错误。
if(exception.getStatus() === 500) {
response
.status(500)
.json({
status: '500',
code: 'INTERNAL_SERVER_ERROR',
message: 'Internal Server Error'
});
}
但是,现在已经设计了一个HTML页面,显示内部服务器错误消息。渲染该页面所需要做的就是点击URL /ui/internal-server-error
。所以,我尝试使用下面的代码。
response
.status(500)
.redirect('/ui/internal-server-error');
当发生内部服务器错误时,页面会加载,但是问题是,当我在浏览器中读取网络日志时,我没有获得500状态。相反,我的状态为304 Not modified
。
有人能指出我正确的方向吗?我想显示错误页面以及状态代码500,并且UI页面仅来自Frontend,因为我无法对其进行访问。
[调用redirect
时,它会设置状态,因此500被替换。
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.
res.redirect('/foo/bar')
res.redirect('http://example.com')
res.redirect(301, 'http://example.com')
res.redirect('../login')
将所需状态作为参数添加到redirect
。