mirror of
https://codeberg.org/buzzcode2007/FCC-Project-URLShortener.git
synced 2025-05-21 03:06:34 +00:00
Update index.js web server class
This commit is contained in:
parent
e16d41f8e1
commit
02709cccf4
1 changed files with 36 additions and 14 deletions
50
index.js
50
index.js
|
@ -1,24 +1,46 @@
|
||||||
|
/* Import modules */
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// Basic Configuration
|
const ShortenAPI = require('./scripts/API').ShortenAPI;
|
||||||
const port = process.env.PORT || 3000;
|
|
||||||
|
|
||||||
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) {
|
constructor(callback) {
|
||||||
res.sendFile(process.cwd() + '/views/index.html');
|
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
|
var listener = app.listen(WebServer.port, () => {
|
||||||
app.get('/api/hello', function(req, res) {
|
console.log(`Active on port ${listener.address().port}.`);
|
||||||
res.json({ greeting: 'hello API' });
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
app.listen(port, function() {
|
/* Configure the default responses for the web server. */
|
||||||
console.log(`Listening on port ${port}`);
|
#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))});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue