From 0cca63354247ee1b91dc27e0bb941dc8861df29c Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sat, 20 Oct 2018 20:27:05 -0700 Subject: [PATCH] Support dynamic route Signed-off-by: James Ketrenos --- server/app.js | 2 +- server/routes/index.js | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/server/app.js b/server/app.js index 26d89cb..d7eede7 100755 --- a/server/app.js +++ b/server/app.js @@ -266,7 +266,7 @@ app.use(basePath + "api/v1/albums", require("./routes/albums")); app.use(basePath + "api/v1/scan", require("./routes/scan")(scanner)); /* Declare the "catch all" index route last; the final route is a 404 dynamic router */ -app.use(basePath + "/*", index); +app.use(basePath, index); /** * Create HTTP server and listen for new connections diff --git a/server/routes/index.js b/server/routes/index.js index 253ca00..a6a9627 100755 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -2,8 +2,7 @@ const express = require("express"), fs = require("fs"), - url = require("url"), - config = require("config"); + url = require("url"); const router = express.Router(); @@ -30,13 +29,24 @@ const extensionMatch = new RegExp("^.*?(" + extensions.join("|") + ")$", "i"); * 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*/) { +router.get("/*", function(req, res, next) { const parts = url.parse(req.url), basePath = req.app.get("basePath"); + console.log("Index check for " + req.url + " with user as " + req.user); + + /* 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 (req.url == "/" || !extensionMatch.exec(parts.pathname)) { + console.log("Returning index for " + req.url); + /* Replace in index.html with - * the basePath */ + * the basePath */ const index = fs.readFileSync("frontend/index.html", "utf8"); res.send(index.replace(