forked from snxraven/ravenscott-blog
Add sitemap route
This commit is contained in:
parent
13cf107559
commit
2b696a93de
53
app.js
53
app.js
@ -5,6 +5,7 @@ const fs = require('fs');
|
|||||||
const { marked } = require('marked');
|
const { marked } = require('marked');
|
||||||
const nodemailer = require('nodemailer');
|
const nodemailer = require('nodemailer');
|
||||||
const hljs = require('highlight.js');
|
const hljs = require('highlight.js');
|
||||||
|
const { format } = require('date-fns'); // To format dates in a proper XML format
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@ -202,6 +203,58 @@ app.get('/blog/:slug', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// 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'));
|
||||||
|
|
||||||
|
// Static URLs (e.g., homepage, about, contact)
|
||||||
|
const staticUrls = [
|
||||||
|
{ url: '/', changefreq: 'weekly', priority: 1.0 },
|
||||||
|
{ url: '/about', changefreq: 'monthly', priority: 0.8 },
|
||||||
|
{ url: '/contact', changefreq: 'monthly', priority: 0.8 }
|
||||||
|
];
|
||||||
|
|
||||||
|
// Dynamic URLs (e.g., blog posts)
|
||||||
|
const blogUrls = blogFiles.map(file => {
|
||||||
|
const title = file.replace('.md', '');
|
||||||
|
const slug = titleToSlug(title);
|
||||||
|
|
||||||
|
// Get the last modified date of the markdown file
|
||||||
|
const stats = fs.statSync(path.join(__dirname, 'markdown', file));
|
||||||
|
const lastModifiedDate = format(new Date(stats.mtime), 'yyyy-MM-dd');
|
||||||
|
|
||||||
|
return {
|
||||||
|
url: `/blog/${slug}`,
|
||||||
|
lastmod: lastModifiedDate,
|
||||||
|
changefreq: 'monthly',
|
||||||
|
priority: 0.9
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Combine static and dynamic URLs
|
||||||
|
const urls = [...staticUrls, ...blogUrls];
|
||||||
|
|
||||||
|
// Generate the XML for the sitemap
|
||||||
|
let sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n`;
|
||||||
|
urls.forEach(({ url, lastmod, changefreq, priority }) => {
|
||||||
|
sitemap += ` <url>\n`;
|
||||||
|
sitemap += ` <loc>${hostname}${url}</loc>\n`;
|
||||||
|
if (lastmod) {
|
||||||
|
sitemap += ` <lastmod>${lastmod}</lastmod>\n`;
|
||||||
|
}
|
||||||
|
sitemap += ` <changefreq>${changefreq}</changefreq>\n`;
|
||||||
|
sitemap += ` <priority>${priority}</priority>\n`;
|
||||||
|
sitemap += ` </url>\n`;
|
||||||
|
});
|
||||||
|
sitemap += `</urlset>`;
|
||||||
|
|
||||||
|
// Set the content type to XML and send the response
|
||||||
|
res.header('Content-Type', 'application/xml');
|
||||||
|
res.send(sitemap);
|
||||||
|
});
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.3",
|
"body-parser": "^1.20.3",
|
||||||
"bootstrap": "^5.3.3",
|
"bootstrap": "^5.3.3",
|
||||||
|
"date-fns": "^4.0.0",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"ejs": "^3.1.10",
|
"ejs": "^3.1.10",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user