nodejs-website/main.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-10-06 07:05:46 -05:00
#!/usr/bin/node
'use strict';
/*
2022-09-14 Hyperling
Coding my own website rather than using WordPress or anything bloaty.
2022-10-06 07:05:46 -05:00
*/
//// Libraries ////
2022-10-06 07:05:46 -05:00
let express = require('express');
let app = express();
const execSync = require('child_process').execSync;
const fs = require('fs');
//// Global Variables ////
2022-10-06 07:05:46 -05:00
const DEBUG = false;
2022-10-06 07:05:46 -05:00
const app_name = "hyperling.com";
let pages = [];
const pages_dir = "./pages/";
const file_types = ["php", "sh"];
2022-10-06 07:05:46 -05:00
let ports = [];
ports.push(8080);
//// Functions ////
2022-10-06 07:05:46 -05:00
async function main() {
console.log("...Starting Main...");
2022-10-06 07:05:46 -05:00
console.log("Set app to return HTML documents.");
2022-10-06 22:15:36 -05:00
app.use(function (req, res, next) {
res.contentType('text/html');
next();
});
2022-10-06 07:05:46 -05:00
/* Loop through all file in the pages subdirectory and add the allowed
// file types into the pages array for automatic router creation.
*/
console.log("...Starting Main...");
let ls = await fs.promises.readdir(pages_dir);
if (DEBUG) console.log("DEBUG: Results of ls, ", ls);
for (let file of ls) {
let file_test = file.split(".");
let file_name = file_test[0];
let file_type = file_test[file_test.length - 1];
if (file_types.includes(file_type)) {
if (DEBUG) console.log("DEBUG: Hooray!", file, "is being added.");
pages[file_name] = pages_dir + file;
console.log(" * Added page", file);
} else {
if (DEBUG) console.log("DEBUG: ", file, "is not an approved type, skipping.");
}
2022-10-06 22:15:36 -05:00
}
console.log(" * Pages loaded: ", pages);
//return; // Stop execution FORTESTING
console.log("...Adding Routes...");
let router = express.Router();
/* AUTOMATIC METHOD BASED ON OBJECT/ARRAY OF PHP scripts
// Creates routes with the URL of the key and location of the value.
*/
for (let key in pages) {
console.log(" * Creating router for", key);
router.get("/" + key, function (req,res) {
console.log(key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url)
res.send(execSync(pages[key]));
2022-10-06 22:15:36 -05:00
});
}
// Provide css.
router.get('/main.css', function (req, res) {
console.log("css being provided to", req.socket.remoteAddress)
let html = execSync("cat ./pages/main.css");
res.send(html);
});
// Originally a test, now a catch-all redirection to Home!
2022-10-06 22:15:36 -05:00
router.get('/*', function (req, res) {
// WARNING: These are huge so only look when you need to.
2022-10-06 22:15:36 -05:00
//console.log(req);
//console.log(res);
console.log("*wildcard* replying to", req.socket.remoteAddress, "asking for", req.url)
let html = execSync("./pages/home.php");
2022-10-06 22:15:36 -05:00
res.send(html);
});
2022-10-06 07:05:46 -05:00
2022-10-06 22:15:36 -05:00
app.use('', router);
2022-10-06 07:05:46 -05:00
console.log("...Adding Ports...");
2022-10-06 22:15:36 -05:00
ports.forEach(port => {
app.listen(port);
console.log(" * Now listening on port " + port + ".");
});
2022-10-06 07:05:46 -05:00
console.log("Done! Now we wait...");
}
//// Program Execution ////
2022-10-06 07:05:46 -05:00
main();