Friday, 9 June 2017

NodeJS issues generating dynamic sitemap from mysql

I am currently trying to create the sitemap for my site but facing a few issues. The sitemap will need to consist of dynamic Urls that are generating as a result of a query to the mysql database.

Below is the code I am using...

router.get('/sitemap.xml', (req, res) => {
        var mysql = require('mysql');
        var connection = mysql.createConnection(process.env.JAWSDB_URL);
        const sqlQuery = 'SELECT DISTINCT(`Make`) FROM Data';

        connection.query(sqlQuery, function(err, data){
            let sitemap; 

            if (err) {
                console.log(err);
            } else {
                sitemap += '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
                sitemap += '<url><loc>http://www.website.co.uk</loc><changefreq>weekly</changefreq><priority>0.5</priority></url>';

                for(var i = 0; i < data.length; i++){
                    const make = data[i]['Make'];
                    const makeUrl = 'http://www.website.co.uk/' + make;
                    sitemap += '<url><loc>' + makeUrl + '</loc><changefreq>daily</changefreq><priority>0.3</priority></url>';

                    const queryString = 'SELECT DISTINCT(`Model`) FROM Data WHERE `Make` = "' + make + '"';
                    connection.query(queryString, function(err2, content){
                        if (err2) {
                            console.log(err2);
                        } else {
                            for(var j = 0; j < content.length; j++){
                                const model = content[j]['Model'];
                                const modelUrl = 'http://www.website.co.uk/' + make + '/' + model;
                                sitemap += '<url><loc>' + modelUrl + '</loc><changefreq>daily</changefreq><priority>0.3</priority></url>';
                                console.log(1);
                            }
                        }
                    });
                }

                connection.end();

                sitemap += '</urlset>';

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

At the moment it is producing a sitemap that only consists of the home Url and all the Urls for the makes i.e. http://www.website.co.uk/. It does not generate all the urls for the models too.

Having done some research and looked at similar issues/topics, I thought my solution should work.

Any help is much appreciated.



via Phil

No comments:

Post a Comment