'ExpressJS redirection/sendFile with assets

In the express server, we have a catch-all for rendering errors that looks like this:

res.status(500).sendFile('error.html', { root: path.join(__dirname, '../public') });

Which will send the user to a "404"-looking HTML page, while setting the status to 500 (for application insights filtering for these types of errors).

However, it seems like using sendFile keeps the same URL that the user is currently at, while displaying error.html. This is a problem, since we use relative paths in error.html (i.e., ./icons/logo.png).

Since sendFile keeps the same path for the user, it then looks at a URL like site.com/some/path/the/user/is/at/icons/logo.png instead of site.com/icons/logo.png.

So, how can I:

  1. Keep the current URL the user is on
  2. Display error.html
  3. Have the asset paths in error.html be correct?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source