Node.js Express Domain or Website XML Sitemap Generator Online Tool Website Deployed on Website + Source Code 2020

Node.js Express Domain or Website XML Sitemap Generator Online Tool Website Deployed on Website + Source Code 2020

 

 

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

 

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)

 

READ  jsPDF + Html2Canvas Tutorial to Add Arabic Custom Font inside PDF Document in Javascript Full Project For Beginners

 

 

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

 

 

<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

READ  Node.js Express Tutorial to Transform Raw XML to PDF Document Using xml-pdf Module Full Project For Beginners

 

 

 

 

 

 

Leave a Reply