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