From 3fa33e670c12cd232a01ecd483409bd76ebfa3d1 Mon Sep 17 00:00:00 2001 From: liutristan <113070063+liutristan@users.noreply.github.com> Date: Wed, 10 May 2023 18:38:00 -0400 Subject: [PATCH] Error Handling + Use of ES6 Destructuring + Logging --- index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 5060925..db809a5 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,21 @@ import express from 'express'; -import * as url from 'url'; -import * as path from 'node:path'; +import { fileURLToPath } from 'url'; +import { join } from 'node:path'; const app = express(); -const port = process.env.port || 8080; -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); +const port = process.env.PORT || 8080; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); -app.use(express.static(path.join(__dirname, '/static'), { extensions: ['html'] })); -app.use((req, res) => res.status(404).sendFile(path.join(__dirname, './static/', '404.html'))); +app.use(express.static(join(__dirname, '/static'), { extensions: ['html'] })); + +app.use((req, res) => res.status(404).sendFile(join(__dirname, './static/', '404.html'))); + +// Error handling middleware +app.use((err, req, res, next) => { + console.error(err.stack); + res.status(500).send('Something broke!'); +}); const server = app.listen(port, () => { console.log(`Polaris is running on port ${server.address().port}, using nodejs ${process.version}`); -}) \ No newline at end of file +});