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();
|
||||
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))});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue