From 3cd55961cb5f9207ce53e0324bb7a9d132bbd54c Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Thu, 20 Mar 2025 02:00:42 +0000 Subject: [PATCH] Initiate web server --- index.js | 59 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index c3bebad..6e783c6 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,49 @@ // index.js -// where your node app starts // init project require('dotenv').config(); -var express = require('express'); +const 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 -// http://expressjs.com/en/starter/static-files.html -app.use(express.static('public')); +const ParserAPI = require('./scripts/ParserAPI').ParserAPI; -// http://expressjs.com/en/starter/basic-routing.html -app.get('/', function (req, res) { - res.sendFile(__dirname + '/views/index.html'); -}); +class WebServer { + /* + Initiate the web server. + + @param {function} callback - The callback run before activating the server + */ + + constructor(callback) { + app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204 + + + this.#setDefaults(); + (callback) ? this[`activity`] = callback() : null; + + var listener = app.listen(process.env.PORT || 3000, () => { + 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(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 ParserAPI(app))}); -// your first API endpoint... -app.get('/api/hello', function (req, res) { - res.json({ greeting: 'hello API' }); -}); -// listen for requests :) -var listener = app.listen(process.env.PORT || 3000, function () { - console.log('Your app is listening on port ' + listener.address().port); -});