From 2ec229b1f68b31b0e43c7d307e14fa290095ae24 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:25:09 +0000 Subject: [PATCH] feat: Create web server --- index.js | 64 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index bde5ff7..e879953 100644 --- a/index.js +++ b/index.js @@ -5,28 +5,50 @@ var express = require('express'); var app = express(); -// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) -// so that your API is remotely testable by FCC -var cors = require('cors'); -app.use(cors({optionsSuccessStatus: 200})); // some legacy browsers choke on 204 +const Messaging = require('./scripts/messaging').Messaging; +var TimestampAPI = require('./scripts/timestampAPI').TimestampAPI; -// http://expressjs.com/en/starter/static-files.html -app.use(express.static('public')); +class WebServer { + /* + Initiate the web server. -// http://expressjs.com/en/starter/basic-routing.html -app.get("/", function (req, res) { - res.sendFile(__dirname + '/views/index.html'); -}); + @param {function} callback - The callback run before initiating the server + */ + constructor(callback) { + // enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) + // so that your API is remotely testable by FCC + var cors = require('cors'); + app.use(cors({optionsSuccessStatus: 200})); // some legacy browsers choke on 204 + + this.#setDefaults(); + + if (callback) { + this[`activity`] = callback(); + } + + // Listen on port set in environment variable or default to 3000 + var listener = app.listen(process.env.PORT || 3000, function () { + console.log(`Active on port ${listener.address().port}.`); + }); + } + + /* Configure the default responses for the web server. */ + #setDefaults() { + // http://expressjs.com/en/starter/static-files.html + app.use(WebServer.paths['assets'], express.static(__dirname + WebServer.paths['assets'])); + + // http://expressjs.com/en/starter/basic-routing.html + app.get("/", function (REQUEST, RESPONSE) { + RESPONSE.sendFile(__dirname + WebServer[`paths`][`default`]); + }); + } + + static paths = { + "assets": "/public", + "default": '/views/index.html' + } +} + +new WebServer(() => {return (new TimestampAPI(app))}); -// your first API endpoint... -app.get("/api/hello", function (req, res) { - res.json({greeting: 'hello API'}); -}); - - - -// Listen on port set in environment variable or default to 3000 -var listener = app.listen(process.env.PORT || 3000, function () { - console.log('Your app is listening on port ' + listener.address().port); -});