Auto generate Sitemap on React Js using Node js and Msql

I finished upgrading my website and was in need of a sitemap. Sitemaps a what make your website seo friendly, gives robots command on when to revisit your website. here is my solution I used to do that.

You can check my auto generated sitemap and this was used on a mernstack application

First of all install the node js sitemap

npm install sitemap

After the installation, create the below function.

const genSiteMap = () => {}

Inside that function, add this lines of code.

function genSiteMap(db) {
  const $getPosts = `SELECT * from posts`;
  // Creates a sitemap object given the input configuration with URLs
  const sitemap = new SitemapStream({ hostname: "" });
  // Location to the output of your sitemap.xml -> your react static build folder
  const writeStream = createWriteStream("./build/sitemap.xml");
  try {
    // Here am using the nodejs myql
    // All the data for my posts from the database are gotten from here
    db.query($getPosts, (err, data) => {
      if (data.length > 0) {
        // Construcst my sitemap object
        // And added the priority
        const maps = => ({
          url: `/blog/${m.slug}`,
          changefreq: "daily",
          priority: 0.6,
        // Loop throuh the object and write them to the sitemap
        var i;
        for (i = 0; i < data.length; i++) {
        // Close the stream
  } catch (err) {

Then finally, use the method where so ever you wish to, and your sitemap will be generated automatically on every new item added to your website. Do make sure you share to your friends.