From 41e7ef9aa051620676dba62881ac7d00656adf4c Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Wed, 22 Apr 2020 09:57:28 -0700 Subject: [PATCH] Routing works between webpack and app, game loading works, new game creation works Signed-off-by: James Ketrenos --- index.html | 1 + server/app.js | 11 ++++------- server/basepath.js | 9 +++++++++ server/console-line.js | 2 +- server/mail.js | 0 server/routes/games.js | 5 +++-- server/routes/index.js | 17 +++++++++-------- src/App.js | 12 +++++++++++- src/Board.js | 30 ++++++++++++++++++++++++++---- src/history.js | 11 +++++++++++ webpack.dev.js | 15 ++++++++++----- 11 files changed, 85 insertions(+), 28 deletions(-) create mode 100755 server/basepath.js mode change 100644 => 100755 server/console-line.js mode change 100644 => 100755 server/mail.js create mode 100755 src/history.js mode change 100644 => 100755 webpack.dev.js diff --git a/index.html b/index.html index c22200c..e3e18d6 100755 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" /> + diff --git a/server/app.js b/server/app.js index d0a07ac..4c1ba2f 100755 --- a/server/app.js +++ b/server/app.js @@ -10,19 +10,14 @@ const express = require("express"), config = require("config"), session = require('express-session'), hb = require("handlebars"), - SQLiteStore = require('connect-sqlite3')(session); + SQLiteStore = require('connect-sqlite3')(session), + basePath = require("./basepath"); require("./console-line.js"); /* Monkey-patch console.log with line numbers */ const frontendPath = config.get("frontendPath").replace(/\/$/, "") + "/", serverConfig = config.get("server"); -let basePath = config.get("basePath"); -basePath = "/" + basePath.replace(/^\/+/, "").replace(/\/+$/, "") + "/"; -if (basePath == "//") { - basePath = "/"; -} - console.log("Hosting server from: " + basePath); let userDB, gameDB; @@ -231,6 +226,8 @@ app.use(basePath, function(req, res, next) { /* Allow loading of the app w/out being logged in */ app.use(basePath, index); +/* /games loads the default index */ +app.use(basePath + "games", index); /* Allow access to the 'users' API w/out being logged in */ const users = require("./routes/users"); diff --git a/server/basepath.js b/server/basepath.js new file mode 100755 index 0000000..b4dd8cf --- /dev/null +++ b/server/basepath.js @@ -0,0 +1,9 @@ +const config = require("config"); + +let basePath = config.get("basePath"); +basePath = "/" + basePath.replace(/^\/+/, "").replace(/\/+$/, "") + "/"; +if (basePath == "//") { + basePath = "/"; +} + +module.exports = basePath; diff --git a/server/console-line.js b/server/console-line.js old mode 100644 new mode 100755 index fb47e05..a6f33b9 --- a/server/console-line.js +++ b/server/console-line.js @@ -15,7 +15,7 @@ if (process.env.LOG_LINE) { } let err = getErrorObject(), - caller_line = err.stack.split("\n")[4], + caller_line = err.stack.split("\n")[3], args = [caller_line.replace(cwdRe, "$1 -")]; /* arguments.unshift() doesn't exist... */ diff --git a/server/mail.js b/server/mail.js old mode 100644 new mode 100755 diff --git a/server/routes/games.js b/server/routes/games.js index de86977..af39329 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -100,7 +100,7 @@ for (let i = 0; i < 5; i++) { const games = {}; router.get("/:id", (req, res/*, next*/) => { - console.log("GET /" + req.params.id); + console.log("GET games/" + req.params.id); if (req.params.id in games) { return res.status(200).send(games[req.params.id]); } else { @@ -110,7 +110,7 @@ router.get("/:id", (req, res/*, next*/) => { }); router.post("/", (req, res/*, next*/) => { - console.log("POST /"); + console.log("POST games/"); const game = { startTime: Date.now(), tiles: [], @@ -150,6 +150,7 @@ router.post("/", (req, res/*, next*/) => { games[game.id] = game; + console.log(`New game created: ${game.id}`); return res.status(200).send(game); }); diff --git a/server/routes/index.js b/server/routes/index.js index b55885d..d8cee3e 100755 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -3,7 +3,8 @@ const express = require("express"), fs = require("fs"), url = require("url"), - config = require("config"); + config = require("config"), + basePath = require("../basepath"); const router = express.Router(); @@ -31,24 +32,24 @@ const extensionMatch = new RegExp("^.*?(" + extensions.join("|") + ")$", "i"); * 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"); + const parts = url.parse(req.url); /* 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 != "/") { + if (!req.user && + req.url != "/" && + req.url.indexOf("/games") != 0) { return next(); } - if (req.url == "/" || !extensionMatch.exec(parts.pathname)) { + if (req.url == "/" || req.url.indexOf("/games") == 0 || !extensionMatch.exec(parts.pathname)) { console.log("Returning index for " + req.url); - /* Replace in index.html with + /* Replace in index.html with * the basePath */ const frontendPath = config.get("frontendPath").replace(/\/$/, "") + "/", - index = fs.readFileSync(frontendPath + "/index.html", "utf8"); - + index = fs.readFileSync(frontendPath + "index.html", "utf8"); res.send(index.replace( /