简体   繁体   English

如何使用Node / Express在特定错误上显示自定义html页面?

[英]How to show custom html page on specific errors with Node/Express?

Frequently, whenever we deploy, our servers will return 503s for up to 2-3 minutes as assets are compiled. 通常,无论何时部署,随着资产的编译,我们的服务器将在2-3分钟内返回503s。 How is it possible with Express/Node to serve a static html page if the server code comes back 503? 如果服务器代码返回503,Express / Node如何提供静态html页面? Surely there must be an easy way to listen for specific error codes? 当然,必须有一种简单的方法来侦听特定的错误代码吗?

app.use an error handler as your last middleware: app.use 使用错误处理程序作为最后的中间件:

app.use(function (err, req, res, next) {
  if (res.statusCode === 503)
    return res.render('errors/503.jade');

  next(); // default Express' error handler.
});

Copied the format directly from Express js Page. 直接从Express js Page复制了格式。

var bodyParser = require('body-parser');
var methodOverride = require('method-override');

        app.use(bodyParser());
        app.use(methodOverride());
        app.use(function(err, req, res, next) {

if (res.status === 503)
{
res.sendFile('yourfilepath/filename.html');
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angularjs中有条件地显示自定义html页面和基于视图的页面 - How to show a custom html page and view based page conditionally in angularjs 如何优雅地捕获节点和快递中的 jsonwebtoken 错误? - How to gracefully catch jsonwebtoken errors in node & express? 如何处理Node.js / express中的错误 - How to handle errors in Node.js/express Node.js 和 typescript; 如何捕捉表达错误 - Node js with typescript; How to catch express errors 如何在HTML页面上显示错误 - How to display errors on the html page 在Node.js中使用Express成功后,如何发送JSON响应并重定向到某些html页面? - How to send a JSON response and redirect to some html page after success in Node.js using express? 在 Node.js / Express 中,我如何“下载”页面并获取其 HTML? - In Node.js / Express, how do I “download” a page and gets its HTML? 如何将html渲染到页面中,而不是整个视图渲染到Node.js + express应用程序中? - How to render html into a page but not as the entire view into an Node.js + express app? 如何使用node.js express在特定目录的特定路径上呈现HTML页面 - How to render html page on certain path from certain directory with node.js express 如何在HTML(通过节点JS / Express)页面中显示和设置JSON数据样式 - How do i display & style JSON data in my HTML (via node JS/Express) page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM