kurumkan
kurumkan

Reputation: 2725

How to handle all routes express

There is a nodejs (express) app. It has few router handlers. I want the app after getting SIGTERM to respond with 500 status to all the routes. How can I achieve that? I tried:

process.on('SIGTERM', () => {
    console.log('SIGTERM CATCHED');

    app.use((req, res) => {
        console.log('RESPONDING PROPERLY')
        res.sendStatus(500);
    })
});

But that would definitely not override the more specific handlers. Additionally, I shouldn't shutdown the app.

Upvotes: 1

Views: 53

Answers (1)

sangaloma
sangaloma

Reputation: 422

You can use a session variable that you set to 'true' once a SIGTERM is caught (catched if you want :) ). Then you define a middleware that tests if this session variable is false then next() else res.sendStatus(500).

Upvotes: 1

Related Questions