Update index.js web server class

This commit is contained in:
buzzcode2007 2025-03-23 07:47:03 +00:00
parent e16d41f8e1
commit 02709cccf4

View file

@ -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))});