diff --git a/index.js b/index.js index f7e45be..ad157aa 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,46 @@ +/* Import modules */ require('dotenv').config(); const express = require('express'); const cors = require('cors'); const app = express(); -// Basic Configuration -const port = process.env.PORT || 3000; +const ShortenAPI = require('./scripts/API').ShortenAPI; -app.use(cors()); +class WebServer { + // Basic Configuration + static port = process.env.PORT || 3000; + + /* + Initiate the web server. -app.use('/public', express.static(`${process.cwd()}/public`)); + @param {function} callback - The callback run before activating the server + */ -app.get('/', function(req, res) { - res.sendFile(process.cwd() + '/views/index.html'); -}); + constructor(callback) { + app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204 + // app.use(cors()); + + this.#setDefaults(); + (callback) ? this[`activity`] = callback() : null; -// Your first API endpoint -app.get('/api/hello', function(req, res) { - res.json({ greeting: 'hello API' }); -}); + var listener = app.listen(WebServer.port, () => { + console.log(`Active on port ${listener.address().port}.`); + }); + } -app.listen(port, function() { - console.log(`Listening on port ${port}`); -}); + /* Configure the default responses for the web server. */ + #setDefaults() { + app.use(WebServer.paths['assets'], express.static(__dirname + WebServer.paths['assets'])); + + app.get("/", function (REQUEST, RESPONSE) { + RESPONSE.sendFile(__dirname + WebServer[`paths`][`default`]); + }); + } + + static paths = { + "assets": "/public", + "default": '/views/index.html' + } +} + +new WebServer(() => {return (new ShortenAPI(app))});