From b23e3071b675da10343b6e88cfd5e2c0e24448a9 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 25 Sep 2024 23:52:55 -0400 Subject: [PATCH] uodate route --- app.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 868ff4f..cd64cde 100644 --- a/app.js +++ b/app.js @@ -206,8 +206,18 @@ app.post('/contact', async (req, res) => { } }); -// Blog Post Route +// Blog Post Route for new URLs (without /blog prefix) +app.get('/:slug', (req, res) => { + handleBlogPost(req, res); +}); + +// Blog Post Route for old URLs (with /blog prefix) app.get('/blog/:slug', (req, res) => { + handleBlogPost(req, res); +}); + +// Function to handle blog posts (used in both routes) +function handleBlogPost(req, res) { const slug = req.params.slug; const markdownFile = fs.readdirSync(path.join(__dirname, 'markdown')) .find(file => titleToSlug(file.replace('.md', '')) === slug); @@ -226,9 +236,7 @@ app.get('/blog/:slug', (req, res) => { } else { res.redirect('/'); // Redirect to the home page if the blog post is not found } -}); - -// 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