Node.js Express Bulk Domain Age Checker App Built Using Javascript & AJAX Full Tutorial with Source Code 2020

Node.js Express Bulk Domain Age Checker App Built Using Javascript & AJAX Full Tutorial with Source Code 2020

 

 

 

Live Demo

 

 

You can see the live demo of the app here

 

 

 

Screenshots

 

 

 

 

 

 

 

Get Started

 

 

First of all create a brand new node.js project by invoking this command

 

npm init -y

 

 

In order to get started you need to install these dependencies inside your node.js project

 

 

npm i express

 

npm i ejs

 

npm i psl

 

npm i whois-json

 

npm i moment

 

npm i is-valid-domain

 

npm i nodemon

 

 

After installing all these dependencies a package.json file will be created and inside it your dependencies will be listed out

 

Now create a index.js file which will be the starting file of the application and copy paste the following code to it

 

 

const express = require('express')

const bodyparser = require('body-parser')

const whoisinfo = require('whois-json')

const moment = require('moment')

const isValidDomain = require('is-valid-domain')

const psl = require('psl')

const app = express()

app.set('view engine','ejs')

app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json())

const PORT = process.env.PORT || 4000

app.get('/',(req,res) => {
    res.render('bulkdomainagechecker',{title:"Bulk Domain Age Checker"})
})

app.post('/getbaseurl',(req,res) =>{

var parsed = psl.parse(req.body.domain);

console.log(parsed.domain)

res.json({

baseurl:parsed.domain

})


})


app.post('/singledomainagechecker',async(req,res) => {
  var url = req.body.domain;


  if (!isValidDomain(url)) {
    res.json({
      domain:url,
      domainage:"Invalid Domain"
    })
  } else {
    var results = await whoisinfo(url);

    var date = moment(results.creationDate).format("YYYY-MM-DD");
    var currentDate = moment(new Date()).format("YYYY-MM-DD");

    console.log(date);
    console.log(currentDate);

    var a = moment(date);
    var b = moment(currentDate);

    var years = b.diff(a, "year");
    a.add(years, "years");

    var months = b.diff(a, "months");
    a.add(months, "months");

    var days = b.diff(a, "days");

    var domainAge = years + " years " + months + " months " + days + " days";

    res.json({
      domain:url,
      domainage:domainAge
    })
  }
})


app.listen(PORT)

 

See also  Build a HTML5 Apple Catcher or Picking Game From Tree in Browser Using Javascript Full Project For Beginners

 

 

Now we want to create the ejs template for our application. So now create a views folder inside your root directory and create a bulkdomainagechecker.ejs file and copy paste the following code

 

 

views/bulkdomainagechecker.ejs

 

 

 

<!DOCTYPE html>
<html>
  <head>
    <title><%=title%></title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
    <div class="container">
      <h1 class="text-center">Bulk Domain Name Age Checker</h1>
      <form id="form">
        <div class="form-group">
          <label for="domain">Enter Domains URL: (One Per Line)</label>
          <textarea
            class="form-control"
            rows="10"
            cols="20"
            id="domains"
            placeholder="Domain names"
            required
          ></textarea>
        </div>
        <div class="form-group">
          <button id="button" class="btn btn-block btn-danger">
            Get Domain Age
          </button>
        </div>
      </form>
      <div>
        <table class="table table-striped">
          <thead>
            <tr>
              <th>Domain URL</th>
              <th>Domain Age</th>
            </tr>
          </thead>
          <tbody id="body"></tbody>
        </table>
      </div>
      <br /><br />
    </div>
  </body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/psl/1.8.0/psl.min.js"></script>
  <script>
    $("#form").submit(function (e) {
      e.preventDefault();

      var domains = $("#domains").val().split("\n");

      $("#body").empty();

      $("#button").text("Checking Domain Age Please Wait");

      $("#button").prop("disabled", "true");

      console.log(domains);

      domains.forEach((domain) => {
        

        var domain;

        domain = domain
          .replace(/^(?:https?:\/\/)?(?:www\.)?/i, "")
          .split("/")[0];

        $.ajax({
          method: "POST",
          url: "/getbaseurl",
          data: { domain: domain },
          success: function (data) {
            console.log(data.baseurl);

            $.ajax({
              method: "POST",
              url: "/singledomainagechecker",
              data: { domain: data.baseurl },
              success: function (data) {
                console.log(data.domainage);
                $("#button").text("Get Domain Age");

                document.getElementById("button").disabled = false;

                $("#body").append(`
                  <tr>
                     <td>${data.domain}</td>
                     <td>${data.domainage}</td> 
                  </tr>

                  `);
              },
            });
          },
        });
      });
    });
  </script>
</html>

 

See also  Basic CRUD Application in VUE with Firebase

 

 

 

DOWNLOAD SOURCE CODE

 

 

 

Leave a Reply