Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Fixed 404 on /random, edited the router system a bit for simplicity, …
Browse files Browse the repository at this point in the history
…hid the error message for security
  • Loading branch information
TheDogHusky committed Jun 10, 2024
1 parent 168992a commit e765c6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ const main = async () => {
const routes = fs.readdirSync(path.join(__dirname, 'routes')).filter(file => file.endsWith('.js'));
for (const file of routes) {
const route = require(`./routes/${file}`);

if (Array.isArray(route)) {
for (const r of route) {
if(r.path && isValidMethod(r.method)) {
// this code is old, too lazy to rewrite it to use a better router system. Like they say, Don't Touch If It Works!
const run = (req, res) => {
// conditions before run here

return r.router(req, res, logger, {});
};
app[r.method.toLowerCase()](r.path, run);
logger.info(`Loaded route ${r.path} (${r.method.toUpperCase()})`, 'Express');
}
}
continue;
}

if(route.path && isValidMethod(route.method)) {
const run = (req, res) => {
// conditions before run here
Expand Down
34 changes: 21 additions & 13 deletions src/routes/random.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const express = require('express');

module.exports = [
{
path: "/random/:type",
method: "get",
router: (req, res, data, logger) => {
const type = req.params.type;
if (!type) return res.redirect('/random/neko');
if (!['neko', 'kitsune', 'lewd', 'hug', 'kiss', 'pat', 'slap'].includes(type)) return res.redirect('/random/neko');

module.exports = {
path: "/random/:type",
method: "get",
router: (req, res, data, logger) => {
const type = req.params.type;
if (!type) return res.status(400).json({ message: 'Bad request.' });
if (!['neko', 'kitsune', 'lewd', 'hug', 'kiss', 'pat', 'slap'].includes(type)) return res.redirect('/random/neko');

return res.render('random', {
page: req.url,
type: type
});
return res.render('random', {
page: req.url,
type: type
});
}
},
{
path: "/random",
method: "get",
router: (req, res) => {
return res.redirect('/random/neko');
}
}
};
];
2 changes: 1 addition & 1 deletion web/views/404.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="display-1 fw-bold">404</h1>
<p class="fs-3"> <span class="text-danger">Oops!</span> Page not found!</p>
<p class="lead">
The requested content has not been found in this server.
The requested content has not been found on this server.
</p>
<a href="/" class="btn btn-primary">Go Home!</a>
</div>
Expand Down
4 changes: 2 additions & 2 deletions web/views/globalerror.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<h1 class="display-1 fw-bold"><%= status %></h1>
<p class="fs-3"> <span class="text-danger">Oops!</span> An error occured!</p>
<p class="lead">
<%= error.message %>
</p>
An error has occured while processing your request. Please try again later. If the problem persists, please contact the system administrator.
</p>
<a href="/" class="btn btn-primary">Go Home!</a>
</div>
</div>
Expand Down

0 comments on commit e765c6b

Please sign in to comment.