fix rss and website sitemap

This commit is contained in:
Raven Scott 2024-09-18 18:55:52 -04:00
parent 5236c18fe1
commit 9ff7d0bc8c

20
app.js
View File

@ -211,10 +211,17 @@ app.get('/blog/:slug', (req, res) => {
} }
}); });
// Sitemap Route
// Sitemap Route // Sitemap Route
app.get('/sitemap.xml', (req, res) => { app.get('/sitemap.xml', (req, res) => {
const hostname = req.headers.host || 'http://localhost'; // Ensure this is your site URL in production 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) // Static URLs (e.g., homepage, about, contact)
const staticUrls = [ const staticUrls = [
@ -262,10 +269,18 @@ app.get('/sitemap.xml', (req, res) => {
res.send(sitemap); res.send(sitemap);
}); });
// RSS Feed Route
// RSS Feed Route // RSS Feed Route
app.get('/rss', (req, res) => { app.get('/rss', (req, res) => {
const hostname = req.headers.host || 'http://localhost'; // Adjust for production if needed 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 // Build RSS feed
let rssFeed = `<?xml version="1.0" encoding="UTF-8" ?>\n<rss version="2.0">\n<channel>\n`; let rssFeed = `<?xml version="1.0" encoding="UTF-8" ?>\n<rss version="2.0">\n<channel>\n`;
@ -302,6 +317,7 @@ app.get('/rss', (req, res) => {
res.send(rssFeed); res.send(rssFeed);
}); });
// Global 404 handler for any other unmatched routes // Global 404 handler for any other unmatched routes
app.use((req, res) => { app.use((req, res) => {
res.redirect('/'); // Redirect to the home page for any 404 error res.redirect('/'); // Redirect to the home page for any 404 error