[英]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.