From f7fb1c0dd809fee09c582e451c6c636c72ba56b0 Mon Sep 17 00:00:00 2001 From: Geoffrey Wu Date: Wed, 18 Sep 2024 12:05:19 -0400 Subject: [PATCH] simplify routing --- client/script.js | 5 -- routes/about/index.js | 16 ------ routes/admin/category-reports.js | 9 --- routes/admin/index.js | 13 +---- routes/admin/leaderboard.js | 9 --- routes/api-docs.js | 30 ---------- routes/backups.js | 8 --- routes/database.js | 8 --- routes/index.js | 94 +++----------------------------- routes/multiplayer.js | 3 +- routes/settings.js | 8 --- routes/singleplayer/bonuses.js | 8 --- routes/singleplayer/index.js | 14 ----- routes/singleplayer/tossups.js | 8 --- 14 files changed, 12 insertions(+), 221 deletions(-) delete mode 100644 routes/about/index.js delete mode 100644 routes/admin/category-reports.js delete mode 100644 routes/admin/leaderboard.js delete mode 100644 routes/api-docs.js delete mode 100644 routes/backups.js delete mode 100644 routes/database.js delete mode 100644 routes/settings.js delete mode 100644 routes/singleplayer/bonuses.js delete mode 100644 routes/singleplayer/index.js delete mode 100644 routes/singleplayer/tossups.js diff --git a/client/script.js b/client/script.js index 324e395b..2c0dcbe0 100644 --- a/client/script.js +++ b/client/script.js @@ -2,11 +2,6 @@ import account from './scripts/accounts.js'; // Javascript code common to *all* pages of the site. -// Never use trailing slash, except for the root directory -if (window.location.pathname.endsWith('/') && window.location.pathname.length > 1) { - window.location.pathname = window.location.pathname.substring(0, window.location.pathname.length - 1); -} - // Always use www if (['qbreader.herokuapp.com', 'qbreader-production.herokuapp.com', 'qbreader.org'].includes(window.location.hostname)) { window.location.hostname = 'www.qbreader.org'; diff --git a/routes/about/index.js b/routes/about/index.js deleted file mode 100644 index a0a28155..00000000 --- a/routes/about/index.js +++ /dev/null @@ -1,16 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client/about' }); -}); - -router.get('/privacy-policy', (req, res) => { - res.sendFile('privacy-policy.html', { root: './client/about' }); -}); - -router.get('/terms-of-service', (req, res) => { - res.sendFile('terms-of-service.html', { root: './client/about' }); -}); - -export default router; diff --git a/routes/admin/category-reports.js b/routes/admin/category-reports.js deleted file mode 100644 index b24408ef..00000000 --- a/routes/admin/category-reports.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Router } from 'express'; - -const router = Router(); - -router.get('/', (_req, res) => { - res.sendFile('index.html', { root: './client/admin/category-reports' }); -}); - -export default router; diff --git a/routes/admin/index.js b/routes/admin/index.js index 051821f2..86a3a23e 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -1,9 +1,7 @@ import isAdmin from '../../database/account-info/is-admin.js'; import { checkToken } from '../../server/authentication.js'; -import categoryReportsRouter from './category-reports.js'; import geowordRouter from './geoword.js'; -import leaderboardRouter from './leaderboard.js'; import { Router } from 'express'; @@ -11,7 +9,6 @@ const router = Router(); router.use(async (req, res, next) => { const { username, token } = req.session; - if (!checkToken(username, token)) { delete req.session; res.redirect('/user/login?' + encodeURIComponent(req.originalUrl)); @@ -19,21 +16,13 @@ router.use(async (req, res, next) => { } const admin = await isAdmin(username); - if (!admin) { - res.redirect('/user/login'); - return; + return res.redirect('/user/login'); } next(); }); -router.use('/category-reports', categoryReportsRouter); router.use('/geoword', geowordRouter); -router.use('/leaderboard', leaderboardRouter); - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client/admin' }); -}); export default router; diff --git a/routes/admin/leaderboard.js b/routes/admin/leaderboard.js deleted file mode 100644 index 46fcbd80..00000000 --- a/routes/admin/leaderboard.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Router } from 'express'; - -const router = Router(); - -router.get('/', async (req, res) => { - res.sendFile('index.html', { root: './client/admin/leaderboard' }); -}); - -export default router; diff --git a/routes/api-docs.js b/routes/api-docs.js deleted file mode 100644 index 73b39295..00000000 --- a/routes/api-docs.js +++ /dev/null @@ -1,30 +0,0 @@ -import { Router } from 'express'; -import * as fs from 'fs'; - -const router = Router(); -const docsDir = './client/api-docs'; - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: docsDir }); -}); - -// routes every html doc in /client/api-docs -fs.readdirSync(docsDir).forEach(file => { - if (file.endsWith('.html') && file !== 'index.html') { - const route = file.substring(0, file.length - 5); - router.get(`/${route}`, (req, res) => { - res.sendFile(file, { root: docsDir }); - }); - } -}); - -fs.readdirSync(`${docsDir}/multiplayer`).forEach(file => { - if (file.endsWith('.html') && file !== 'index.html') { - const route = file.substring(0, file.length - 5); - router.get(`/multiplayer/${route}`, (req, res) => { - res.sendFile(file, { root: docsDir + '/multiplayer' }); - }); - } -}); - -export default router; diff --git a/routes/backups.js b/routes/backups.js deleted file mode 100644 index d964e124..00000000 --- a/routes/backups.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (_req, res) => { - res.sendFile('backups.html', { root: './client' }); -}); - -export default router; diff --git a/routes/database.js b/routes/database.js deleted file mode 100644 index 73daaba1..00000000 --- a/routes/database.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client/database' }); -}); - -export default router; diff --git a/routes/index.js b/routes/index.js index c97e6fbc..cb0228f8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,117 +1,41 @@ -import aboutRouter from './about/index.js'; import adminRouter from './admin/index.js'; import apiRouter from './api/index.js'; -import apiDocsRouter from './api-docs.js'; import authRouter from './auth/index.js'; -import backupsRouter from './backups.js'; -import databaseRouter from './database.js'; import frequencyListRouter from './frequency-list.js'; import geowordRouter from './geoword/index.js'; import multiplayerRouter from './multiplayer.js'; -import settingsRouter from './settings.js'; -import singleplayerRouter from './singleplayer/index.js'; import userRouter from './user.js'; import webhookRouter from './api/webhook.js'; -import { Router } from 'express'; +import express, { Router } from 'express'; const router = Router(); -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client' }); -}); - -router.get('/react(-dom)?/umd/*.js', (req, res) => { - res.sendFile(req.url, { root: './node_modules' }); -}); - -router.get('/react-highlight-words/build/static/*.js', (req, res) => { - res.sendFile(req.url, { root: './node_modules' }); -}); - -router.get('/node_modules/*.scss', (req, res) => { - res.sendFile(req.url.substring(13), { root: './node_modules' }); -}); - -router.get('/*.css', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - -router.get('/*.ico', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - -router.get('/*.js', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - -router.get('/*.jsx', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - -router.get('/*.map', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - -router.get('/*.png', (req, res) => { - res.sendFile(req.url, { root: './client' }); -}); - router.get('/*.scss', (req, res) => { - res.sendFile(req.url.substring(5), { root: './scss' }); + res.sendFile(req.url, { root: './scss' }); }); /** * Redirects: */ -router.get('/*.html', (req, res) => { - res.redirect(req.url.substring(0, req.url.length - 5)); -}); - -router.get('/api-info', (req, res) => { - res.redirect('/api-docs'); -}); - -router.get('/bonuses', (_req, res) => { - res.redirect('/singleplayer/bonuses'); -}); - -router.get('/database', (_req, res) => { - res.redirect('/db'); -}); - -router.get('/index', (req, res) => { - res.redirect('/'); -}); - -router.get('/tossups', (req, res) => { - res.redirect('/singleplayer/tossups'); -}); - -router.get('/users', (req, res) => { - res.redirect(`/user${req.url}`); -}); +router.get('/api-info', (req, res) => res.redirect('/api-docs')); +router.get('/bonuses', (req, res) => res.redirect('/singleplayer/bonuses')); +router.get('/db', (req, res) => res.redirect('/database')); +router.get('/tossups', (req, res) => res.redirect('/singleplayer/tossups')); +router.get('/user', (req, res) => res.redirect('/user/login')); /** * Routes: */ - -router.use('/about', aboutRouter); router.use('/admin', adminRouter); router.use('/api', apiRouter); -router.use('/api-docs', apiDocsRouter); router.use('/auth', authRouter); -router.use('/backups', backupsRouter); -router.use('/db', databaseRouter); router.use('/frequency-list', frequencyListRouter); router.use('/geoword', geowordRouter); router.use('/multiplayer', multiplayerRouter); -router.use('/settings', settingsRouter); -router.use('/singleplayer', singleplayerRouter); router.use('/user', userRouter); router.use('/webhook', webhookRouter); -router.use((req, res) => { - res.sendFile(req.url, { root: './client' }); -}); +router.use(express.static('client', { extensions: ['html'] })); +router.use(express.static('node_modules')); export default router; diff --git a/routes/multiplayer.js b/routes/multiplayer.js index d4c069af..edf0adfd 100644 --- a/routes/multiplayer.js +++ b/routes/multiplayer.js @@ -5,7 +5,8 @@ router.get('/', (req, res) => { res.sendFile('index.html', { root: './client/multiplayer' }); }); -router.get('/*', (req, res) => { +// only match paths that don't have a file extension +router.get(/^\/[^.]*$/, (req, res) => { res.sendFile('room.html', { root: './client/multiplayer' }); }); diff --git a/routes/settings.js b/routes/settings.js deleted file mode 100644 index 39c9f58e..00000000 --- a/routes/settings.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client/settings' }); -}); - -export default router; diff --git a/routes/singleplayer/bonuses.js b/routes/singleplayer/bonuses.js deleted file mode 100644 index c55e1b5c..00000000 --- a/routes/singleplayer/bonuses.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('bonuses.html', { root: './client/singleplayer' }); -}); - -export default router; diff --git a/routes/singleplayer/index.js b/routes/singleplayer/index.js deleted file mode 100644 index ae1acc56..00000000 --- a/routes/singleplayer/index.js +++ /dev/null @@ -1,14 +0,0 @@ -import tossupsRouter from './tossups.js'; -import bonusesRouter from './bonuses.js'; - -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('index.html', { root: './client/singleplayer' }); -}); - -router.use('/bonuses', bonusesRouter); -router.use('/tossups', tossupsRouter); - -export default router; diff --git a/routes/singleplayer/tossups.js b/routes/singleplayer/tossups.js deleted file mode 100644 index 81f93aa7..00000000 --- a/routes/singleplayer/tossups.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Router } from 'express'; -const router = Router(); - -router.get('/', (req, res) => { - res.sendFile('tossups.html', { root: './client/singleplayer' }); -}); - -export default router;