How to make a Health Site Bot
Tags: webdev, javascript, development, node
Published: 2023-03-28 12:53:30

---
title: How to make a Health Site Bot
published: true
date: 2023-03-22 22:15:12 UTC
tags: webdevelopment,javascript,development,nodejs
canonical_url: https://marcellecode.medium.com/how-to-make-a-health-site-bot-9ef6e00b53b9
---

![](https://cdn-images-1.medium.com/max/1024/1*9fl1MOhXze49L0vNvgsrSw.png)
_banner how to make a health site bot_

### When we create a website be it for a client or personal, it’s important to guarantee the service is up. Thinking help with this, that’s the objective of this article. With that in mind, let’s create a bot using Node.js to let us know when our site responds to HTTP Status other than 200.

### Step 1: Start the project

The first step of our project is to create the folder of our bot, so insert this line of code below on your terminal

```
mkdir health-site-bot && cd health-site-bot
```

Now we’ll run a command for Node.js to create the basic structure necessary to run the project

```
npm init -y
```

If everything is ok, now we’ll be creating the two files to define our bot.

```
touch config.js && touch server.js
```

### Step 2: Install the Libraries

Run all the lines below on your terminal

```
npm i dotenv && npm i express && npm i nodemailer
```

### Step 3: Editing Config.js

Let’s import the dotenv and path libraries, and export the global variables EMAIL and PASSWORD.

```
const dotenv = require("dotenv");
const path = require("path");

dotenv.config({
  path: ".env",
});

module.exports = {
  EMAIL: process.env.EMAIL,
  PASS: process.env.SENHA
};
```

### Step 4: Create a .env file

The .env file is a file that saves information like credentials. With this method, we can make configurations for the production and development environments just set what .env file I want to use.

```
#.env
EMAIL=email@teste.com
PASS=123456
```

How the .env file serves to store credentials it’s not so good if you save this on your repository. In the next step, we’ll configure the .gitignore to remove this file from our repository and prevent accidental commits.

### Step 5: Configure .gitignore file

In this file we’ll add a name of two files for git not add when we make a commit.

```
touch .gitignore

node_modules
.env
```

### Step 6: Create a serve.js file

The serve.js is a file we use to configure our bot. So, the first thing we’ll do is, import the necessary libraries.

```
//serve.js
const nodemailer = require("nodemailer");
const config = require("./config.js");
const moment = require("moment");
const https = require("https");
```

After that, we create a constant variable for storing the URL application we want to monitor.

```
const url = "https://www.google.com/"
```

With this URL defined, we can create a function for making requests HTTP to verify the status. If this status is not equal to 200, we’ll call a function to notify us.

```
function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}
```

In this tutorial, our bot notifies us by e-mail. So, let’s create a function called “\_sendMail()”

```
function _sendMail(statusHTTP) {
  var transporter = nodemailer.createTransport({
    host: "HOST_EMAIL",
    auth: {
      user: config.EMAIL,
      pass: config.SENHA,
    },
  });
}
```

In the above code, we use a function from nodemailer to create a connection with e-mail host. The username and password used in this file are imported from the .env file, they are the email account data we will use to send the notifications.

```
var mailOptions = {
    from: "YOUR_EMAIL",
    to: "EMAIL_RECEIVE",
    subject: `ERROR WITH ${url}`,
    text: `Oops! Your service is having problems.
           Problem description:
           Received code: ${statusHTTP}`,
  };
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    }
  });
```

The last part of our script is to create a function for call \_sendMail if the website return status different than 200.

```
function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}

healthCheck();
```

At this moment, the serve.js file is like this

```
const nodemailer = require("nodemailer");
const config = require("./config.js");
const https = require("https");

const _url = 'URL_APP';

function _sendMail(statusHTTP) {
  var transporter = nodemailer.createTransport({
    host: "HOST_EMAIL",
    auth: {
      user: config.EMAIL,
      pass: config.SENHA,
    },
  });
  var mailOptions = {
    from: "SEU_EMAIL",
    to: "EMAIL_DO_REMETENTE",
    subject: `ERROR WITH ${url}`,
    text: `Oops! Your service is having problems.
           Problem description:
           Received code: ${statusHTTP}`,
  };
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    }
  });
}

function healthCheck() {
  https.get(_url, function (res) {
    if (res.statusCode !== 200) {
      _sendMail();
    }
  });
}

healthCheck();
```

[MarcelleCode is digital content creator about web development](http://www.buymeacoffee.com/marcellecode)