j_d
j_d

Reputation: 3082

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. How is it possible with Express/Node to serve a static html page if the server code comes back 503? Surely there must be an easy way to listen for specific error codes?

Upvotes: 2

Views: 237

Answers (2)

Gandalf the White
Gandalf the White

Reputation: 2465

Copied the format directly from 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');
    }

});

Upvotes: 1

Aleksei Zabrodskii
Aleksei Zabrodskii

Reputation: 2228

app.use an error handler as your last middleware:

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

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

Upvotes: 2

Related Questions