Home > OS >  Render page before process.exit()
Render page before process.exit()

Time:09-11

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?

CodePudding user response:

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;
  • Related