modify web server index
This commit is contained in:
parent
3618cbbddb
commit
c0c0085294
1 changed files with 39 additions and 12 deletions
51
index.js
51
index.js
|
@ -1,18 +1,45 @@
|
|||
const express = require('express')
|
||||
const app = express()
|
||||
const cors = require('cors')
|
||||
require('dotenv').config()
|
||||
/* Import modules */
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const app = express();
|
||||
|
||||
app.use(cors())
|
||||
app.use(express.static('public'))
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/views/index.html')
|
||||
});
|
||||
const MainAPI = require('./scripts/API.JS');
|
||||
|
||||
class WebServer {
|
||||
// Basic Configuration
|
||||
static port = process.env.PORT || 3000;
|
||||
|
||||
/*
|
||||
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
|
||||
// app.use(cors());
|
||||
|
||||
this.#setDefaults();
|
||||
(callback) ? this[`activity`] = callback() : null;
|
||||
|
||||
var listener = app.listen(WebServer.port, () => {
|
||||
console.log(`Active on port ${listener.address().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 MainAPI(app))});
|
||||
|
||||
const listener = app.listen(process.env.PORT || 3000, () => {
|
||||
console.log('Your app is listening on port ' + listener.address().port)
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue