From d26f5b26a711b28d7a712c1bdeb40e2906f19c59 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 3 Sep 2023 16:23:07 +0100 Subject: [PATCH] Renders error page, if there was an error --- server.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index bb1a386..f0bf39d 100644 --- a/server.js +++ b/server.js @@ -11,6 +11,7 @@ const port = process.env.PORT || 3000; // The port to run the server on const API_DIR = '/api'; // Name of the dir containing the lambda functions const dirPath = path.join(__dirname, API_DIR); // Path to the lambda functions dir const guiPath = path.join(__dirname, 'build'); +const placeholderFilePath = path.join(__dirname, 'public', 'placeholder.html'); const handlers = {}; // Will store list of API endpoints process.env.WC_SERVER = 'true'; // Tells middleware to return in non-lambda mode @@ -95,23 +96,34 @@ app.use(historyApiFallback({ // Serve up the GUI - if build dir exists, and GUI feature enabled if (process.env.DISABLE_GUI && process.env.DISABLE_GUI !== 'false') { - app.get('*', (req, res) => { - res.status(500).send( - 'Welcome to Web-Check!
Access the API endpoints at ' - +'/api' - ); + app.get('*', async (req, res) => { + const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8'); + const htmlContent = placeholderContent.replace( + '', + 'Web-Check API is up and running!
Access the endpoints at ' + +'/api' + ); + + res.status(500).send(htmlContent); }); } else if (!fs.existsSync(guiPath)) { - app.get('*', (req, res) => { - res.status(500).send( - 'Welcome to Web-Check!
Looks like the GUI app has not yet been compiled, ' - +'run yarn build to continue, then restart the server.' + app.get('*', async (req, res) => { + const placeholderContent = await fs.promises.readFile(placeholderFilePath, 'utf-8'); + const htmlContent = placeholderContent.replace( + '', + 'Looks like the GUI app has not yet been compiled.
' + + 'Run yarn build to continue, then restart the server.' ); + res.status(500).send(htmlContent); }); } else { // GUI enabled, and build files present, let's go!! app.use(express.static(guiPath)); } +app.use((req, res, next) => { + res.status(404).sendFile(path.join(__dirname, 'public', 'error.html')); +}); + // Print nice welcome message to user const printMessage = () => { console.log(