How to create sitemap.xml with Sailsjs

I am trying to figure out how to have a sitemap with sails.js

I tried using sitemap-express, but it seems to be for expressing routing.

+3


source to share


1 answer


Although this question is quite old, but I would like to post a solution here for seekers, for example I landed here.

First you need to install sitemap-generator

Let's say I have a route 'get /sitemap' : 'SitemapController.build_sitemap',

SitemapController.js



    sitemap: function(req, res) {
       var fs = require('fs');
       
       var SitemapGenerator = require('sitemap-generator');
       var generator = new SitemapGenerator('https://your_website.com/');
       
       generator.on('done', function (sitemap) {
           console.log(sitemap);

           fs.writeFile("./assets/sitemap.xml", sitemap, function(err){
               if(err) {
                   return res.negotiate(err);
               }
               console.log("The file was saved!");
               return;  
           });
       });
       
       generator.start();
   },

      

At this point, a sitemap.xml file will be created in the directory. / assets (you can put it wherever you want, its choice / decision is made)

NOW , that you already have the .xml in your project, all that remains is to return / map the sitemap.xml file to the GET route.

thank

+2


source







All Articles