浏览器未显示状态为500

时间:2020-04-08 17:13:40

标签: node.js redirect nestjs

我的应用程序中有两个组件。基于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,因为我无法对其进行访问。

1 个答案:

答案 0 :(得分:0)

调用redirect时,它会设置状态,因此500被替换了。

来自the express docs(与NestJS默认使用Express相关):

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

相关问题