From 044be2a8c6e27415f8cd7deab0725f9c192cd313 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 7 Mar 2024 00:28:36 -0500 Subject: [PATCH] Fix a few things related to running pages and trying to read directories. --- commands/app.js | 31 ++++++++++++++++++------------- knexfile.cjs | 2 +- {static => pages}/index.html | 16 ++-------------- 3 files changed, 21 insertions(+), 28 deletions(-) rename {static => pages}/index.html (90%) diff --git a/commands/app.js b/commands/app.js index 9b901fa..092b2c6 100644 --- a/commands/app.js +++ b/commands/app.js @@ -52,19 +52,24 @@ const configure = async (fastify, opts) => { app.register(Formbody); app.get("/*", (req, rep) => { - const target = path.resolve(path.join("pages", req.url)); - const index = path.resolve(path.join("pages", req.url, "index.html")); - - if(fs.existsSync(index)) { - const result = nunjucks.render(index); - rep.code(200).type("text/html").send(result); - } else if(fs.existsSync(target)) { - const fname = req.url.slice(1); - const result = nunjucks.render(fname); - rep.code(200).type("text/html").send(result); - } else { - console.error("404 NOT FOUND", target, "NO INDEX", index); - rep.code(404).type("text/html").send("Not Found"); + try { + const target = path.resolve(path.join("pages", req.url)); + const index = path.resolve(path.join("pages", req.url, "index.html")); + + if(fs.existsSync(index) && !fs.statSync(index).isDirectory()) { + const result = nunjucks.render(index); + rep.code(200).type("text/html").send(result); + } else if(fs.existsSync(target)) { + const fname = req.url.slice(1); + const result = nunjucks.render(fname); + rep.code(200).type("text/html").send(result); + } else { + console.error("404 NOT FOUND", target, "NO INDEX", index); + rep.code(404).type("text/html").send("Not Found"); + } + } catch(error) { + console.error(error); + rep.code(500).type("text/html").send("Internal Server Error"); } }); diff --git a/knexfile.cjs b/knexfile.cjs index 81ffbb6..518d242 100644 --- a/knexfile.cjs +++ b/knexfile.cjs @@ -6,7 +6,7 @@ module.exports = { client: 'sqlite3', useNullAsDefault: true, connection: { - filename: './dev.sqlite3' + filename: './db/dev.sqlite3' } }, diff --git a/static/index.html b/pages/index.html similarity index 90% rename from static/index.html rename to pages/index.html index 896bed0..a217dc4 100644 --- a/static/index.html +++ b/pages/index.html @@ -1,16 +1,5 @@ - - - - - - - - Bandolier2 - +{% include "./header.html" %}