Initiate web server

This commit is contained in:
buzzcode2007 2025-03-20 02:00:42 +00:00
parent 7c74514f4f
commit 3cd55961cb

View file

@ -1,30 +1,49 @@
// index.js // index.js
// where your node app starts
// init project // init project
require('dotenv').config(); require('dotenv').config();
var express = require('express'); const express = require('express');
var app = 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'); var cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204
// http://expressjs.com/en/starter/static-files.html const ParserAPI = require('./scripts/ParserAPI').ParserAPI;
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html class WebServer {
app.get('/', function (req, res) { /*
res.sendFile(__dirname + '/views/index.html'); 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);
});