diff --git a/main.js b/main.js index 966fe5c..4e865e8 100755 --- a/main.js +++ b/main.js @@ -33,9 +33,9 @@ const asset_types = ['png', 'svg', 'ico', 'webmanifest', 'jpg']; let ports = []; // Check parameters for numeric port numbers. process.argv.forEach(function (val, index, array) { - console.log("Parameter", index + ':', val, !isNaN(val)); + console.log(now() + ": Parameter", index + ':', val, !isNaN(val)); if (!isNaN(val)) { - console.log("Adding Port", val) + console.log(now() + ": Adding Port", val) ports.push(val); } }); @@ -96,16 +96,21 @@ function getMIME(extension) { return mime; } +// Print datetime for logging. +function now() { + return new Date().toISOString(); +} + /* Code exists inside a main function so that we may use async/await. */ async function main() { - console.log("...Starting Main..."); + console.log(now() + ": ...Starting Main..."); // Getting dates in Node.js is awful, just use Linux. const start_datetime = "" + await execSync('date "+%Y-%m-%dT%H:%M:%S%:z"'); const start_datetime_trimmed = start_datetime.trim(); - console.log("Set app to return HTML documents."); + console.log(now() + ": Set app to return HTML documents."); app.use(function (req, res, next) { res.contentType('text/html'); next(); @@ -114,7 +119,7 @@ async function main() { /* Loop through all files in the pages subdirectory and add the allowed // file types into the pages array for automatic router creation. */ - console.log("...Starting Main..."); + console.log(now() + ": ...Starting Pages..."); let ls = await fs.promises.readdir(pages_dir); if (DEBUG) console.log("DEBUG: Results of ls, ", ls); for (let file of ls) { @@ -124,18 +129,19 @@ async function main() { 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); + console.log(now() + ": * Added page", file); } else { if (DEBUG) console.log("DEBUG: ", file, "is not an approved type, skipping."); } } - console.log(" * Pages loaded: ", pages); + console.log(now() + ": * Pages loaded: ", pages); //return; // Stop execution FORTESTING /* Loop through the assets folder and grab favicons, manifests, etc. and // allow them to be accessed via the root of the website, such as a favicon // at 'https://hyperling.com/favicon.ico'. May help with SEO! */ + console.log(now() + ": ...Starting Assets..."); let ls_assets = await fs.promises.readdir(asset_dir); if (DEBUG) console.log("DEBUG: Results of ls_assets, ", ls_assets); for (let file of ls_assets) { @@ -145,12 +151,12 @@ async function main() { if (asset_types.includes(file_type)) { if (DEBUG) console.log("DEBUG: Hooray!", file, "is being added."); assets[file] = asset_dir + file; - console.log(" * Added asset", file); + console.log(now() + ": * Added asset", file); } else { if (DEBUG) console.log("DEBUG: ", file, "is not an approved type, skipping."); } } - console.log(" * Assets loaded: ", assets); + console.log(now() + ": * Assets loaded: ", assets); /* Create both an XML and HTML sitemap based on these entries. XML is used for // bots like SEO scrapers. HTML will be for human users looking for a list of @@ -212,7 +218,7 @@ async function main() { Sitemap: https://hyperling.com/sitemap.xml `; - console.log("...Adding Routes..."); + console.log(now() + ": ...Adding Routes..."); let router = express.Router(); /* AUTOMATIC METHOD BASED ON OBJECT/ARRAY OF WEB SCRIPTS @@ -220,9 +226,9 @@ async function main() { */ // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" for (let key in pages) { - console.log(" * Creating router for", key); + console.log(now() + ": * Creating router for", key); router.get("/" + key, async function (req,res) { - console.log(key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url); + console.log(now() + ":", key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url); let html = "" + execSync(pages[key]); stringsToRemove.forEach(string => { html = html.replace(string, ""); @@ -233,7 +239,7 @@ async function main() { /* Append the page to the sitemap variables. */ - console.log(" * * Adding to sitemap.xml"); + console.log(now() + ": * * Adding to sitemap.xml"); sitemap_xml = sitemap_xml + ` https://hyperling.com/`+key+`/ @@ -241,7 +247,7 @@ async function main() { 0.80 `; - console.log(" * * Adding to sitemap.html"); + console.log(now() + ": * * Adding to sitemap.html"); sitemap_html = sitemap_html + `
  • `+key+` @@ -260,11 +266,11 @@ async function main() { `; - console.log(" * Creating routers for assets"); + console.log(now() + ": * Creating routers for assets"); for (let key in assets) { - console.log(" * * Adding /" + key); + console.log(now() + ": * * Adding /" + key); router.get('/' + key, function (req, res) { - console.log("asset response to", req.socket.remoteAddress, "asking for", req.url) + console.log(now() + ": asset response to", req.socket.remoteAddress, "asking for", req.url) // Build variables. const file = "./assets/" + req.path; @@ -286,34 +292,34 @@ async function main() { // Provide sitemap.xml file for "SEO". // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" - console.log(" * Creating router for sitemap.xml"); + console.log(now() + ": * Creating router for sitemap.xml"); router.get('/sitemap.xml', function (req, res) { - console.log("sitemap.xml being provided to", req.socket.remoteAddress) + console.log(now() + ": sitemap.xml being provided to", req.socket.remoteAddress) res.contentType('text/xml'); res.send(sitemap_xml); }); // Provide human-usable sitemap links. // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" - console.log(" * Creating router for sitemap*"); + console.log(now() + ": * Creating router for sitemap*"); router.get('/sitemap*', async function (req, res) { - console.log("sitemap.html being provided to", req.socket.remoteAddress) + console.log(now() + ": sitemap.html being provided to", req.socket.remoteAddress) html = await mini(sitemap_html); res.send(mini(html)); }); // Provide a robots.txt for where search engines should find things. - console.log(" * Creating router for robots.txt"); + console.log(now() + ": * Creating router for robots.txt"); router.get('/robots.txt', function (req, res) { - console.log("robots.txt being provided to", req.socket.remoteAddress) + console.log(now() + ": robots.txt being provided to", req.socket.remoteAddress) res.send(robots_txt); }); // Return a resource from the files folder. // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" - console.log(" * Creating router for files"); + console.log(now() + ": * Creating router for files"); router.get('/files*', function (req, res) { - console.log("file response to", req.socket.remoteAddress, "asking for", req.url) + console.log(now() + ": file response to", req.socket.remoteAddress, "asking for", req.url) // Build variables. const file = "." + req.path; @@ -333,12 +339,12 @@ async function main() { }); // Originally a test, now a catch-all redirection to Home! - console.log(" * Creating router for redirection"); + console.log(now() + ": * Creating router for redirection"); router.get('/*', async function (req, res) { // WARNING: These are huge so only look when you need to. //console.log(req); //console.log(res); - console.log("*wildcard* replying to", req.socket.remoteAddress, "asking for", req.url) + console.log(now() + ": *wildcard* replying to", req.socket.remoteAddress, "asking for", req.url) let html = "" + execSync("./pages/home.php"); stringsToRemove.forEach(string => { html = html.replace(string, ""); @@ -349,13 +355,13 @@ async function main() { app.use('', router); - console.log("...Adding Ports..."); + console.log(now() + ": ...Adding Ports..."); ports.forEach(port => { app.listen(port); - console.log(" * Now listening on port " + port + "."); - console.log(" * URL: http://localhost:" + port); + console.log(now() + ": * Now listening on port " + port + "."); + console.log(now() + ": * URL: http://localhost:" + port); }); - console.log("Done! Now we wait..."); + console.log(now() + ": Done! Now we wait..."); } //// Program Execution ////