"use strict"; const express = require("express"), fs = require("fs"), url = require("url"); const router = express.Router(); /* List of filename extensions we know are "potential" file extensions for * assets we don"t want to return "index.html" for */ const extensions = [ "html", "js", "css", "eot", "gif", "ico", "jpeg", "jpg", "mp4", "md", "ttf", "txt", "woff", "woff2", "yml", "svg" ]; /* Build the extension match RegExp from the list of extensions */ const extensionMatch = new RegExp("^.*?(" + extensions.join("|") + ")$", "i"); /* To handle dynamic routes, we return index.html to every request that * gets this far -- so this needs to be the last route. * * However, that introduces site development problems when assets are * referenced which don't yet exist (due to bugs, or sequence of adds) -- * the server would return HTML content instead of the 404. * * So, check to see if the requested path is for an asset with a recognized * file extension. * * If so, 404 because the asset isn't there. otherwise assume it is a * dynamic client side route and *then* return index.html. */ router.get("/*", function(req, res, next) { const parts = url.parse(req.url), basePath = req.app.get("basePath"); /* If req.user isn't set yet (authentication hasn't happened) then * only allow / to be loaded--everything else chains to the next * handler */ if (!req.user && req.url != "/") { return next(); } if (extensionMatch.exec(parts.pathname)) { return res.status(404).send({ message: "File not found", status: 404 }); } let source; console.log(JSON.stringify(parts, null, 2)); switch (parts.pathname) { case "/": source = "frontend/index.html"; break; case "/identities": source = "./index.html"; break; } if (source) { console.log("Returning " + source + " for " + req.url); /* Replace in index.html with * the basePath */ const index = fs.readFileSync(source, "utf8"); return res.send(index.replace( /