浏览器未显示状态为500的内部服务器错误HTML页面

问题描述 投票:0回答:1

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

node.js redirect nestjs
1个回答
0
投票

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

来自the express docs

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

© www.soinside.com 2019 - 2024. All rights reserved.