fix/middleware preventing 404 redirect

This commit is contained in:
SowinskiBraeden committed 2025-05-07 13:01:24 -07:00
1 parent f417ac00fb
commit cadbc2187d
3 files changed
+42 -2

No files matched your search

+7 -1
View File
@@ -84,6 +84,12 @@ app.post('/api/location', async (req,res) => {
res.json(data); res.json(data);
}); });
// 404 handler - keep the actual notFound route please
app.get('/notFound', (req, res) => {
res.render('notFound');
return res.status(status.NotFound);
});
// Initialize database and start app // Initialize database and start app
initDatabase().then(() => { initDatabase().then(() => {
console.log("Successfully connected to MongoDB"); console.log("Successfully connected to MongoDB");
@@ -97,7 +103,7 @@ initDatabase().then(() => {
// 404 handler // 404 handler
app.get('/*splat', (req, res) => { app.get('/*splat', (req, res) => {
res.send('404 Not Found'); res.render('notFound');
return res.status(status.NotFound); return res.status(status.NotFound);
}); });
+11 -1
View File
@@ -1,12 +1,22 @@
const status = require("../util/statuses"); const status = require("../util/statuses");
const session = require("express-session"); const session = require("express-session");
// Get all user routes names
let userRouter = require("../router/user")((req, res, next) => next(), null, null, null);
userRouter.stack.shift();
const userRoutes = userRouter.stack.map((layer) => layer.route.path.split("/")[1]);
/** /**
* @param {MongoClient.collection} users db collection * @param {MongoClient.collection} users db collection
* @returns {async function} middleware handler function * @returns {async function} middleware handler function
*/ */
const createMiddleware = (users) => { const createMiddleware = (users) => {
return async (req, res, next) => { return async (req, res, next) => {
// Redirect not found pages to 404 page
if (!userRoutes.includes(req.url.substring(1))) {
return res.status(status.NotFound).redirect("/notFound");
}
if (!req.session.authenticated || !req.session.email) { if (!req.session.authenticated || !req.session.email) {
req.session.errMessage = "Please login to view that resource"; req.session.errMessage = "Please login to view that resource";
res.redirect("/login"); res.redirect("/login");
+24
View File
@@ -0,0 +1,24 @@
<%- include("./partials/fileHeader") %>
<%- include("./partials/headerStart") %>
<main>
<section class="bg-center bg-no-repeat bg-cover bg-[url('/images/bg1.jpg')] bg-gray-400 bg-blend-multiply">
<div class="px-4 mx-auto max-w-screen-xl text-center py-24 lg:py-56">
<h1 class="mb-4 text-4xl font-extrabold tracking-tight leading-none text-white md:text-5xl lg:text-6xl">404 - Not found</h1>
<p class="mb-8 text-lg font-normal text-gray-300 lg:text-xl sm:px-16 lg:px-48">It appears you stumbled accross a misleading page.</p>
<div class="flex flex-col space-y-4 sm:flex-row sm:justify-center sm:space-y-0">
<a href="/" class="inline-flex justify-center items-center py-3 px-5 text-base font-medium text-center text-white rounded-lg bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 dark:focus:ring-blue-900">
Go home
<svg class="w-3.5 h-3.5 ms-2 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 10">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M1 5h12m0 0L9 1m4 4L9 9"/>
</svg>
</a>
<a href="/aboutUs" class="inline-flex justify-center hover:text-gray-900 items-center py-3 px-5 sm:ms-4 text-base font-medium text-center text-white rounded-lg border border-white hover:bg-gray-100 focus:ring-4 focus:ring-gray-400">
Learn more
</a>
</div>
</div>
</section>
</main>
<%- include("./partials/footer") %>