我如何在node js中渲染404和500的自定义错误页面

问题描述 投票:0回答:1
app.use(function(req, res, next) {
    if(res.status === 404){
      res.status(404).sendFile(path.join(__dirname, '../website/404.html'));
    }
    else{
      res.status(500).sendFile(path.join(__dirname, '../website/500.html'));
    }
  });

`为什么它只运行 500 错误而不是抛出 404 错误。它改变了 t0 500 错误

正确的语法是什么`

node.js error-handling http-status-code-404 http-status-code-500
1个回答
0
投票

与 404 或 500 比较时使用

res.statusCode
,而不是
res.status

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