yes you can use express-sitemap
To generate Sitemap automatically
var sitemap = require('express-sitemap')();
var app = require('express')();
sitemap.generate(app);
To generate dynamically..
for suppose you have products pages and you have specified url for them..
You can create a dynamic file everytime and place it in your public folder.
const Product = require('./model/product')
const sitemap = require('sitemap');
let sitemapData;
const generateSitemap = async () => {
const products = await Product.find({},{path: 1});
const urls = products.map({path} => `/products/${path}`)
sitemapData = sitemap.createSitemap ({
hostname: 'http://example.com',
cacheTime: 600000, // 600 sec - cache purge period
urls
});
}
You can use this function in a routine or with a cron and generate sitemap regularly..
setInterval(generateSitemap, 360000); //running every hour
other thing that you can do is:
use sitemapData
variable and do things like this.
sitemapData.add({url: '/product-a/'}); // when some product is added
sitemapData.add({url: '/product-b/', changefreq: 'monthly', priority: 0.7});
sitemapData.del({url: '/product-c/'}); // when something is removed
sitemapData.del('/product-d/');
you can serve it in a route like this:
app.get('/sitemap.xml', function(req, res) {
sitemapData.toXML( function (err, xml) {
if (err) {
return res.status(500).end();
}
res.header('Content-Type', 'application/xml');
res.send( xml );
});
});