From 9ff7d0bc8cea3439e76f76aa86c3e841cd798010 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 18 Sep 2024 18:55:52 -0400 Subject: [PATCH] fix rss and website sitemap --- app.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 1bd477c..9fee4ba 100644 --- a/app.js +++ b/app.js @@ -211,10 +211,17 @@ app.get('/blog/:slug', (req, res) => { } }); +// Sitemap Route // Sitemap Route app.get('/sitemap.xml', (req, res) => { const hostname = req.headers.host || 'http://localhost'; // Ensure this is your site URL in production - const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); + const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')) + .filter(file => file.endsWith('.md')) + .sort((a, b) => { + const statA = fs.statSync(path.join(__dirname, 'markdown', a)).mtime; + const statB = fs.statSync(path.join(__dirname, 'markdown', b)).mtime; + return statB - statA; // Sort in descending order (latest first) + }); // Static URLs (e.g., homepage, about, contact) const staticUrls = [ @@ -262,10 +269,18 @@ app.get('/sitemap.xml', (req, res) => { res.send(sitemap); }); + +// RSS Feed Route // RSS Feed Route app.get('/rss', (req, res) => { const hostname = req.headers.host || 'http://localhost'; // Adjust for production if needed - const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); + const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')) + .filter(file => file.endsWith('.md')) + .sort((a, b) => { + const statA = fs.statSync(path.join(__dirname, 'markdown', a)).mtime; + const statB = fs.statSync(path.join(__dirname, 'markdown', b)).mtime; + return statB - statA; // Sort in descending order (latest first) + }); // Build RSS feed let rssFeed = `\n\n\n`; @@ -302,6 +317,7 @@ app.get('/rss', (req, res) => { res.send(rssFeed); }); + // Global 404 handler for any other unmatched routes app.use((req, res) => { res.redirect('/'); // Redirect to the home page for any 404 error