Skip to content

Commit

Permalink
🥅 Adds try catch to server welcome msg (gchq#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lissy93 committed Feb 7, 2022
1 parent 1294b43 commit e5ccdd9
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Note: The app must first be built (yarn build) before this script is run
* This is the main entry point for the application, a simple server that
* runs some checks, and then serves up the app from the ./dist directory
* Also includes some routes for status checks/ ping and config saving
* Also imports some routes for status checks/ ping and config saving
* Note: The app must first be built (yarn build) before this script is run
* */

/* Import built-in Node server modules */
Expand Down Expand Up @@ -46,10 +46,15 @@ const getLocalIp = () => {

/* Gets the users local IP and port, then calls to print welcome message */
const printWelcomeMessage = () => {
getLocalIp().then(({ address }) => {
const ip = address || 'localhost';
console.log(printMessage(ip, port, isDocker)); // eslint-disable-line no-console
});
try {
getLocalIp().then(({ address }) => {
const ip = address || 'localhost';
console.log(printMessage(ip, port, isDocker)); // eslint-disable-line no-console
});
} catch (e) {
// Fetching info for welcome message failed, print simple msg instead
console.log(`Dashy server has started (${port})`); // eslint-disable-line no-console
}
};

/* Just console.warns an error */
Expand All @@ -65,7 +70,7 @@ const app = express()
.use(express.static(path.join(__dirname, 'dist')))
.use(express.static(path.join(__dirname, 'public'), { index: 'initialization.html' }))
// Load middlewares for parsing JSON, and supporting HTML5 history routing
.use(express.json())
.use(express.json({ limit: '1mb' }))
.use(history())
// GET endpoint to run status of a given URL with GET request
.use(ENDPOINTS.statusCheck, (req, res) => {
Expand Down Expand Up @@ -114,7 +119,13 @@ const app = express()
});

/* Create HTTP server from app on port, and print welcome message */
http.createServer(app).listen(port, () => { printWelcomeMessage(); });
http.createServer(app)
.listen(port, () => {
printWelcomeMessage();
})
.on('error', (err) => {
printWarning('Unable to start Dashy\'s Node server', err);
});

/* Check, and if possible start SSL server too */
sslServer(app);

0 comments on commit e5ccdd9

Please sign in to comment.