Welcome folks today in this tutorial we will be building a online tool web app which will be a simple XML Sitemap Generator for the Website or Domain
. All the source code of the application is shown below
Live Demo
You can see the live demo of the online tool here
Get Started
In order to get started we need to install these dependencies
npm init -y
npm i express ejs sitemap-generator
So install all these dependencies inside your node.js project and open it inside a text editor
So now we need to make a simple index.js
file for the application
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
const express = require("express"); const SitemapGenerator = require('sitemap-generator'); const app = express() const bodyparser = require('body-parser') app.use(bodyparser.json()) app.set('view engine','ejs') app.use(bodyparser.urlencoded({extended:false})) app.get('/xmlsitemapgenerator',(req,res) => { res.render('xmlsitemapgenerator',{title:'Generate XML Sitemap for Domain Online - XML Sitemap Generator Online - FreeMediaTools.com'}) }) app.post('/xmlsitemapgenerator',(req,res) => { var url = req.body.url var outputfile = Date.now() + "output.xml" const generator = SitemapGenerator(url, { stripQuerystring: false, filepath:outputfile }); // register event listeners generator.on('done', () => { res.download(outputfile,(err) => { if(err){ fs.unlinkSync(outputfile) res.download("unable to download the sitemap file") } fs.unlinkSync(outputfile) }) }); // start the crawler generator.start(); }) app.listen(5000) |
Creating EJS Templates
Now inside your root directory create the views
folder and create a simple view called xmlsitemapgenerator.ejs
and copy paste the below code
xmlsitemapgenerator.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<div class="container"> <h1 class="text-center"> XML Sitemap Generator </h1> <form action="/xmlsitemapgenerator" method="post"> <div class="form-group"> <label for="url">Website URL:</label> <input class="form-control" type="text" placeholder="google.com" name="url" id="" required> </div> <br> <div class="form-group"> <button class="btn btn-block btn-danger"> Download Sitemap File </button> </div> </form> <br><br> |
Screenshot
Create Sitemaps on Command Line
You can also create xml sitemaps
on the command line also by installing this dependency globally on your machine
npm i -g sitemap-generator-cli
After installing it you just need to execute the command on command line
And now to get sitemap of freemediatools.com
you need to execute the command like
sitemap-generator freemediatools.com
This will create the xml sitemap of this domain in the same folder like this as shown below