I'm trying to use sitemap.js package located at https://www.npmjs.org/package/sitemap
I can push in urls inside the sitemap. But I want to add url based upon the data i retrieve from mongodb. I know how to create the url to feed the sitemap but because finding data from mongo is a callback, before I get data , the router for /sitemap.xml is called so Im not able to feed more urls to the sitemap.
Here is the snapshot of my routes file
var colors = require('colors');
var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var sm = require('sitemap');
var _ = require('underscore');
//these models are found in the /models folder
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
var trendSchema = mongoose.Schema({
tName: String,
tName_h: String,
region: String
});
var Trend = mongoose.model('Trend', trendSchema);
// var Trend = mongoose.model('Trend');
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
var tfind = function (trends) {
Trend.find(function (err, trends) {
if (err) {
console.log(err);
return next(err);
}
console.log('trends =', trends);
var plucked = _.pluck(trends, 'tName');
// console.log('plucked trends',plucked);
// console.log(' sitemap urls = ', sitemap.urls);
// sitemap.urls.length = 0;
// sitemap.urls.push({ url: '/page-10/', changefreq: 'daily', priority: 0.3 });
});
}
tfind.done(function (text) {
console.log(' text = ', text );
});
var sitemap = sm.createSitemap ({
hostname: 'http://example.com',
cacheTime: 600000, // 600 sec - cache purge period
urls: [
{ url: '/page-1/', changefreq: 'daily', priority: 0.3 },
{ url: '/page-2/', changefreq: 'monthly', priority: 0.7 },
{ url: '/page-3/' } // changefreq: 'weekly', priority: 0.5
]
});
router.get('/sitemap.xml', function(req, res) {
sitemap.toXML( function (xml) {
res.header('Content-Type', 'application/xml');
res.send( xml );
});
});
sitemap.urls.push({ url: '/page-5/', changefreq: 'daily', priority: 0.3 });
sitemap.urls.push({ url: '/page-7/', changefreq: 'daily', priority: 0.3 });
// sitemap.urls.push({ url: '/page-9/', changefreq: 'daily', priority: 0.3 });
console.log(' app.js sitemap.urls == ', sitemap.urls);
If I understand your problem correctly, you want to add data fetched from MongoDB into your sitemap before rendering the XML file for response.
First things first, Node.js is asynchronous and MongoDB driver is also based on that. So, your route handle should perform the search within it, and when mongo returns you the documents, only after you are sure that everything is done, then send back the response.
NOTE: For brevity, I'm not writing the config and error handlers here. Also, always put your expressjs config on top, just below the module imports.