Vasily Ivanov Vasily Ivanov - 6 months ago 12
Node.js Question

Node.js : Using sitemaps for virtual hosts (vhost)

I'm a beginner with node.js.
Currently I'm using node.js + express + vhost to host different domains like http://example1.com, http://example2.io etc.

How can I provide a different sitemap for each domain (http://example1.com/sitemap.xml , http://example2.io/sitemap.xml) ?

I see this sitemap-generating framework : sitemap which is not bad actually but I can't figure out how to use it with virtual hosts.

And the same question for robots.txt files for each domain.

Answer

Ok, I get it. sitemap.js do the trick. My code:

const sm = require('sitemap');
const sitemap = sm.createSitemap({
    hostname: 'http://' + req.vhost.host,
    cacheTime: 600000
});
sitemap.add({
    url: '/page1',
    changefreq: 'weekly',
    priority: 1.0
});

res.header('Content-Type', 'application/xml');
res.send(sitemap.toString());
Comments