Render page before process.exit()

Issue

I want to add some error page for my express + ejs app.
What I’m trying to do is..

  1. If there is an error, user will redirected to error page.
  2. After error page contents are displayed on browser, process is killed.
    (Please note that the process should be killed for my case, because of some reason.)

Below code is my current code. But the problem is CSS style, and some object (import from text file througn tag) were not rendered.

I just guess that process.exit() is excuted, before import CSS and object.

app.get('/error', async (req, res) => {
    res.render('error')
    console.log('Connection closed. Please restart installation.')
    process.exit(1)
})

I tried to add callback function in res.render. But the result is same.

Is there any solution to do this?

Solution

Your backend doesn’t know what your frontend requires, so there’s no easy way to wait for the browser to be ready before exiting the process.

A few ad hoc solutions:

  • use setTimeout() and run process.exit() after a few seconds, hoping that that will give the browser enough time to load any requirements;
  • serve all static content (JS/CSS) from a different server process;
  • fix the issue that requires you to exit the process;

Answered By – robertklep

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published