Building Application using Node.js HTTPS Module and Africa’s Talking SMS API

Introduction

For a long time, HTTP (Hypertext Transfer Protocol) was responsible for communication between a client application and a server application. Then came HTTPS (Hypertext Transfer Protocol Secure). HTTPS provides more features compared to HTTP. One of the extra features that HTTPS provides over HTTP is a better security layer. Node.js has an HTTPS module that allows software developers to interface with HTTPS protocol. Using Node.js HTTPS protocol with Africa’s Talking SMS API (An API that allows short message service (SMS) to more than 300 million mobile subscribers across Africa) A developer can develop a more secured and robust application.

Prerequisites

  • GitHub – For hosting our source code. If you do not have a GitHub account, you can create one using this link
  • A basic understanding of JavaScript programming language.
  • A Code Editor – I recommend Visual Studio Code (VS Code).
  • Postman – For testing API endpoints.
  • Africa’s Talking SMS API. Here is the documentation
  • MongoDB – A NoSQL database.
  • Heroku – For hosting our API. If you do not have a Heroku account, you can create one here.
  • Express.js – A Node.js framework for building web applications.
  • Node.js – A JavaScript Run Time
  • Openssl – A general purpose cryptography library.
  • Git – A version control system.

What you will learn

  • How to create an HTTPS server using Node.js HTTPS module.
  • How to integrate Express.js with Node HTTPS module.
  • Testing HTTPS server endpoints using POSTMAN.
  • Integrating MongoDB with Node.js HTTPS module and Express.js
  • Integrating Africa’s Talking SMS API with Node.js HTTPS module, Express.js, and MongoDB
  • Deploying application on Heroku.
  • Building a Todo application using Africa’s Talking SMS API, Node.js HTTPS module, Express.js, and MongoDB.

Intended Audience

  • Anyone that wants to learn how to build web applications using Node.js HTTPS module and Express.js
  • Anyone that wants to learn how to use Africa’s Talking SMS API with Node.js HTTPS module/Express.js
  • Anyone that wants to learn how to integrate MongoDB with Node.js Application/Express.js Applications

Let’s get started

Creating HTTPS server using Node.js

A server is a piece of computer hardware or software (computer program) that provides functionality for other programs or devices, called clients. The architecture is called a client-server model. The clients make request to the server for a resource and the server sends the client the resource.

When a server and a client communicate, they follow a model called the Open Systems Interconnection Model (OSI). This model has several layers (Application Layer, Presentation layer, Session layer, Transport layer, Network Layer, Datalink Layer, and Physical Layer).

Application Layer: This layer specifies the communication protocol.

Presentation Layer: This layer serves as the data translator for the network.

Session Layer: This layer provides mechanism for opening, closing and managing a session between end-user application processes:

Transport layer: The transport layer provides host-to-host communication services for applications.

Network Layer: The network layer provides the means of transferring variable-length network packets from a source to a destination host via one or more networks.

Datalink Layer: This is the Layer that transfers data between nodes on a network segment across the physical layer.

Physical Layer: Physical Layer defines the means of transmitting raw bits over a physical data link connecting network nodes.

HTTP and HTTPS belong to the application layer in the Open System Interconnection Model. As noted in the first section HTTPS is HTTP with security features (TLS and SSL). TLS stands for Transport Layer Security and SSL stands for Secure Socket Layer.

Node.js is a runtime engine for JavaScript. It allows developers to run JavaScript outside the browser. Npm is installed alongside Node.js. Npm is Node.js package manager for Node.js packages. Node.js applications are organized in packages and in modules. A package is a file or directory that is described by a package.json file. A module is any file or directory that can be loaded by the Node.js require () function. Node.js is shipped with modules called core modules. Node.js HTTPS module is a core Node.js module. The HTTPS module is an API for interfacing the HTTPS protocol.

Procedure:

The following are steps to create an HTTPS server using Node.js :

Step 1: Install Node.js:

  • For Mac OS Users:

You can install Node.js on Mac OS using two methods.

(I) NVM (Node Version Manager) (Method 1)

(This method is recommended since it also allows the management of different versions of Node.js)

  • Open the terminal
  • Check if nvm is installed by typing:
      nvm –v

If nvm is installed on the computer, the terminal displays the version of nvm installed on the computer.

  • If nvm is not installed on the computer, we can install it using cURL or Wget, for cURL paste the following in the terminal and hit the return key.
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
  • With Wget : Paste the following in the terminal and hit the return key.
    wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
  • Type in the following in terminal to display the nvm version
    nvm –v

The version of nvm should display in the terminal.

  • Type the following in the terminal to install the latest version of Node.js
    nvm install node
  • Or type the following to install a specific version of Node.js
    nvm install x.x.x

Example nvm install 7.02.01 – This installs version 7.02.01

(ii) Using .pkg installer (Method 2)

  • Using a web browser, visit https://nodejs.org/en/download/
  • Click the .pkg file download link and wait for the file to download.
  • Open the downloaded file and follow the installation instruction to install Node.js.
  • For Linux Users

I recommend installing Node.js using Method 1 for installing Node.js on MacOS.

Open the terminal and type the following to check if Node.js is installed on the computer

     node – v

If the version number is not displayed, Node.js is not installed on the computer. install Node.js following method 1 for installing Node.js on MacOS.

  • For Windows Users
  • Check if Node.js is installed on the computer by typing the following on the command prompt.
    node –v

If Node.js is installed the command prompt will display the version of Node.js installed on the computer.

  • Using a web browser visit https://nodejs.org/en/download/
  • Click Windows Installer (.msi) download link for the architecture (32 bit / 64 bit) of Windows operating for the computer and wait for the file to download.
  • Open the file by double-clicking the file and follow the instruction to install Node.js on the computer.
  • Open the command prompt and type the following in the command prompt:
      node –v

The command prompt should display the version of Node.js

Step 2: create a folder and file

  • Create a directory called httpsafricaapi.
  • Create a file “server.js” inside httpsafricaapi folder. As we progress with the write-up, more files will be added to the httpsafricaapi folder. Check the Github link for this write-up for all the codes.

Step 3: Install Openssl

Openssl is a general-purpose cryptography library for creating security keys

  • Visit https://wiki.openssl.org/index.php/Binaries and download openssl for your computer operating system.
  • Follow the installation instruction and install openssl
  • Open the terminal or command prompt and type the following to check if openssl is installed
     openssl version

The terminal or command prompt should display openssl version.

Step 4: Create a security key

  • Open the terminal or command prompt
  • Navigate to the “httpsafricaapi” folder created in step 2.
  • Create a folder called “keys”
  • Type the following in the terminal:
     openssl genrsa -out keys/server-key.pem 2048

The following are the meaning of the terminal commands;

openssl: openssl command

genrsa: generate RSA (Rivest–Shamir–Adleman) key

-out: outputs the generated key

server-key.pem: this is the file where the key will be stored.

2048: key format

Step 5: Create a Certificate Signing Request.

Certificates are public keys that correspond with a private key. They are digitally signed by the owner of the private key or by a Certificate authority.

Type the following in the terminal to create a Certificate Signing Request for the private key server-key.pem.

      openssl req -new -sha256 -key keys/server-key.pem -out keys/server-csr.pem

The following are the meanings of the terminal command

openssl: openssl command

req: primarily creates and processes certificate requests.

-new: this option generates a new certificate request

-sha256: specifies the message digest to sign the request with.

-key: This specifies where to find the key.

-out: This specifies an output file

keys/server-key.pem: This is the private key

keys/server-csr.pem: This is the output file to write the output.

  • Create a self-signed certificate
openssl x509 -req -in keys/server-csr.pem -signkey keys/server-key.pem -out keys/server- c     ert.pem

openssl: openssl command

X509: outputs self-signed certificate

-req: creates and processes certificate

-in: specifies the input source

-keys/server-csr.pem : certificate signing request

-signkey: specifies the sign in key source

– Keys/server-key.pem : the private key source

-out: specifies the output file

-keys/server-cert.pem : public certificate

Step 6: Open the server.js in the code editor

Step 7: import the HTTPS module

         const https = require('https');

Https module is used for creating HTTPS applications. It is a function object. It contains properties for creating both server and client HTTPS applications.

Step 7: Link the application to the private key and public key using fs module

const fs = require("fs")
const option = {
key: fs.readFileSync("keys/server-key.pem"),
cert: fs.readFileSync("keys/server-cert.pem")
}

In the above code. The private key is “keys/server-key.pem” and the public key is “keys/server-cert.pem”. The private keys and public keys are read using “fs” module from the files server-key.pem and server-cert.pem in the keys folders.

Step 8: Create a server and a request handler

For now, we will be creating a server and a simple request handler that sends “Hello World” to the user.

cosnt server = https.createServer(options, function (req, res) {
res.end("Hello World");
})

The https.createServer() creates the server. It accepts options and a handler as its arguments. The argument “options” is an object that contains information about the private keys and public keys.

Step 9: Add a listening port to the server object created in step 6

server.listen(5000, function() {
console.log("server listening on port 5000")
})

This causes the server object to listen to port 5000.

Step 10: Run the server application

  • Open the terminal or command prompt and navigate to the httpsafricaapi folder.
  • Type the following
     node server.js

The server application starts running

Step 11: Test the server

How to use Express.js

Express.js is a framework of Node.js. It makes building web application easier. Under the hood express actually runs Node.js. The following are steps involved in building web application using express.js.

Step 1: install express.js

Express.js is a Node.js package. It is without strong opinion and keeps things simple. It can be installed using npm. Express.js can be installed per project(locally) or globally on the computer. In this write-up we will be installing Express.js locally. The following are steps to install express.js locally.

  • Open the terminal or command prompt
  • Navigate to the folder httpsafricaapi
  • Type the following the command line or command prompt
      npm init –y

The above command creates a package.json file

  • Type the following in the terminal to install express.js locally in the folder.
     npm install express --save

Step 2: create a file app.js in the folder httpsafricaapis

Step 3: Open app.js in a code editor

Step 4: Import Express.js Package into the program

const express = require("express");

Step 5: create express.js application

const app = express();

The above code snippet creates an express application

Step 6: Create an application middleware

A middleware function is a function that has access to the request object, response object, and next function. In Express there are two types of middleware. These include the application middleware and router middleware. The following creates an application middleware.

app.use("/", (req,res) => {
req.addProperty = 'my name";
console.log("Hello MiddleWare")
next();
})

The above code creates an application middleware which adds addProperty to the request object and logs “Hello Middleware” to the console.

Step 7: Create a route

app.get("/", function(req, res, next) {
res.send("Hello World")
})

Step 8: listen to a port.

let port = 3000;
app.listen(3000, () => {
console.log('server listening at port', port)
});

Step 9: run the app.js application

Type “node app.js” in the command in httpsafricaapi folder. It should log “Hello Middleware” in the console and send “Hello world” to the client making the request.

node app.js

Integrating Express with HTTPS

The following are the steps involved in integrating Express with Node.js HTPS module.

Step 1: navigate to the folder httpsafricaapi.

Step 2: create a file nodeexpress.js

Step 3: open nodeexpress.js in a code editor.

Step 4: require https module:

const https = require('https')

Step 5: require express package.

const express = require('express')

Step 6: require fs module

const fs = require('fs')

Step 7: create express application

const app = express()

Step 8: Create a middleware for the express application in step 7:

app.use(function (req, res, next) {
console.log("Hello Middleware")
next();
})

Step 9: Create a route for the express application in step 7.

app.get("/", function (req, res, next) {
res.send("Hello World");
})

Step 10: create an object “option” linked to the private and public keys created earlier:

const option = {
key: fs.readFileSync("keys/server-key.pem"),
cert: fs.readFileSync("keys/server-cert.pem")
};
const server = https.createServer(option, app)

Step 12: set the server to listen to a port:

let port = 3000;
server.listen(port, function () {
console.log('server listening at port", port);
})

Step 13: Type the following the terminal or command prompt.

node nodeexpress.js

Step 14: Open a browser type the following “https://localhost:3000/ in the address bar and hit return on Mac and Enter on Linux or Windows.

Testing HTTPS server endpoints using POSTMAN

  1. Open Postman Application.
  2. Create a collection:

2.1 In the side bar area click collection tab

2.2 In Collection tab click “New Collection”

2.3 Fill the entries for Name of collection, description, etc.

2.4 Click Create button

  1. Create a folder to group the requests. For REST paradigm approach request can be grouped in per resource.

3.1 In the collection tab selection the collection created in step 2

3.2 Click the “…” icon to open a drop-down icon.

3.3 From the drop down click add folder.

3.4 Fill the entries for Folder name, etc.

  1. Create a request

3.1 Select the collection created in step 2 and click “…” icon to open a drop menu

3.2 From the drop-down menu click add request

3.3 Fill the entries for Request name, request description, etc

4.0 Send Request:

4.1 Select the request from the collection tab.

4.2 Enter the information about the URL in the address bar and click send.

  1. Check the Response

5.1 Check the response

Integrating MongoDB with Node.js HTTPS module and Express.js

MongoDB is a document database. It is a kind of NoSQL Database system. It does not require SQL (structural query language). It uses BSON (Binary JavaScript Object Notation). It scales horizontally.

To use MongoDB with Node.js an object modelling tool is required. Amongst these object modelling tools are Mongoose (npmjs.com/package/mongoose) and MongoDB (https://www.npmjs.com/package/mongodb). But in this write up we are going to be using Mongoose.

Connecting to a MongoDB server from a Node.js Http module and Express application

  1. Ensure that mongoDB is installed on the computer. You can also create an account for mongoDB cloud services (mongoDB atlas). In the test application in this write up we shall be using mongo Atlas
  2. Install the Mongoose

On the terminal or command prompt run the following code.

npm install mongoose –save

  1. Open the code editor and create a file testdb.js
  2. Create MongoDB Atlas account
  3. Copy the URI for your database account from your MongoDB atlas account
  4. Set MONGO_URI=yourURI in a file config.env in the folder config.
  5. Install dotenv running “npm install dotenv”.
  6. Ensure that the file config.env is among the files listed in .gitignore
  7. Paste the following code into testdb.js and ensure :
  1. To test the connection on the terminal or command prompt type the following
          node testdb.js

Integrating Africa’s Talking SMS API with Node.js HTTPS module and Express.js

Africa’s Talking SMS API is an API that allows users to send messages (SMS) to mobile phones from a web application. Africa’s talking SMS API offers a strong integration with Node.JS. Below are steps to use Africa’s Talking with Node.Js Application.

  1. In the command line or command prompt type the following

npm install –save africastalking

  1. Create a file called africaapi.js
  2. Open the file in the code editor
  3. Paste the following inside the file and save
Error when loading gists from https://gist.github.com/.

4.2 In the controller folder create the following files auth.js, todos.js, and users.js

4.3 Paste the following code in auth.js

4.4 Paste the following in todos.js

4.5 Paste the following in users.js

  1. Create config:

5.1 Create a folder in httpsafricaapi called “config”

5.1 In the config folder create the following files config.env and db.js.

5.3 Paste the following code in db.js

Error when loading gists from https://gist.github.com/.

5.4 Paste the following in config.env and enter values for the environmental variables NODE_ENV, PORT, MONGO_URI, JWT_SECRET, JWT_EXPIRE, JWT_COOKIE_EXPIRE, AFRICA_API_KEY, AFRICA_API_USERNAME, AFRICA_API_MESSAGE_TO, AFRICA_API_MESSAGE_FROM

  1. Create Middleware

6.1 Create folder in httpsafricaapi called “middleware”

6.2 In the middleware folder create the following files advancedResults.js , async.js, auth.js, and error.js

6.3 Paste the following code in advancedResults.js

6.4 Paste the following code in async.js

6.5 Paste the following code in auth.js

Error when loading gists from https://gist.github.com/.

6.6 Paste the following code in error.js

  1. Create Models

7.1 Create a folder in httpsafricaapi called “models”.

7.2 In the controller models folder create the following files Todo.js, and User.js

7.3 Paste the following code in Todo.js

7.4 Paste the following code in User.js

  1. Create routes

8.1 Create a folder in httpsafricaapi called “routes”

8.2 In the route folder create the following files auth.js, todos.js, and users.js

8.3 Paste the following code in auth.js

8.4 Paste the following code in todos.js

8.5 Paste the following code in users.js

  1. Create utils

9.1 Create a folder in httpsafricaapi called “utils”

9.2 In the utils folder create the file errorResponse.js

9.3 Paste the following in errorResponse.js

  1. Testing and Documentation of endpoints using postman

10.1 Create a folder in httpsafricaapi called “Public”

10.2 In the Public folder create the file index.html

10.3 Open Postman

10.4 Create a collection called “Todos”.

10.5 Create the following folders Todos, Users, and Authentication in the collection.

10.4 Create the following endpoints following the steps in each of the folders below.

Authentication folders endpoints:

1.Register User
POST /api/v1/auth/register
Content-type: application/json
Sample data:
{   "name": "janet"
    "email": “janet@gmail.com”,
    "password": "1234567"
}
2.Login User
use the Bearer token for registering janet@gmail.com user to login
POST /api/v1/auth/login
Content-type: application/json 
Sample data:
{
    "email": “janet@gmail.com”,
    "password": "1234567"
}
3.Forgot Password 
POST /api/v1/auth/forgotpassword
Content-type: application/json
Sample data:
{
    "email": "janet@gmail.com"
}
4.Reset Password 
copy the link sent as sms into the address bar 
POST http://gkcharlestodo.herokuapp.com/api/v1/auth/resetpassword/d38481b888c5a751b321999d9f4c6951dd9a9e72
Content-type: application/json
Sample data:
{
    "password": "12345678"
}
5.Update Password 
Enter the user login token in the authorization bearer token
POST /api/v1/auth/updatepassword
Content-type: application/json
Sample data:
{
    "currentPassword": “12345678”,
    "newPassword": "123456"
}
6.Update User Details 
Enter the user login token in the authorization bearer token
POST /api/v1/auth/updatedetails
Content-type: application/json
Sample data:
{
    "email": “janet.jackson@gmail.com”,
    "name": "Janet Jackson"
}

Users folder endpoints:

 1.Create User 
Enter the admin login token in the authorization bearer token
-	POST /api/v1/users
-	content-type: application/json
-	Sample data 
{
    "name": " Blessing John",
    "email": "blessingjohn@gmail.com",
    "password": "123456"
}
2. Get all Users 
Enter the admin login token in the authorization bearer token
    • GET /api/v1/users
3.Get Single User 
Enter the admin login token in the authorization bearer token
    • GET /api/v1/users/606cb6016cc8c80015042f06
4.Delete User 
Enter the admin login token in the authorization bearer token
-  DELETE /api/v1/uses/606cb6016cc8c80015042f06
5.Update User 
Enter the admin login token in the authorization bearer token
    • GET /api/v1/users/123
    • content-type: application/json
    • Sample data 
{
    "name": "West John",
    "email": "westjohn@gmail.com"
}

Todo folder endpoints:

1.Create New Todo
Enter the user login token in the authorization bearer token
    • POST /api/v1/todos
    • Content-type: application/json
    • Sample data
{
    "title": "Writing applications",
    "description": "How to write applications",
    "status": "Not Completed",
    "user": "603543bb8c9e100015c5ddc1"
}
2.Get All Todos 
Enter the user login token in the authorization bear token
    • GET /api/v1/todos
3.Delete Todo 
Enter the user login token in the authorization bearer token
    • DELETE /api/v1/todos/606caff96cc8c80015042f05
4.Get Single Todo 
Enter the user login token in the authorization bearer token
    • GET /api/v1/todos/606caf836cc8c80015042f04
5.Update Todo 
Enter the user login token in the authorization bearer token
    • PUT /api/v1/todos/123
    • Sample data:  
{
    "title": "Cooking Salad",
    "description": "Study french fries",
    "status": "Completed"
}

Note: You can find the documentation for the application in this write up in this link

https://gkcharlestodo.herokuapp.com/

10.5 Test the end points by sending request to the endpoints using Postman and ensure that the end points response is okay.

10.6 export the Postman collection and save the exported collection:

10.6.1 In the Todo collection click

10.6.2 Select the location where the file will be saved.

10.6.3 Hit enter key

10.7 Install Docgen

For Mac and Linux users

10.7.1 Open the terminal

10.7.2 Type the following in the terminal and press enter key.

curl https://raw.githubusercontent.com/thedevsaddam/docgen/v3/install.sh -o install.sh \

10.7.3 Type && sudo chmod +x install.sh \ and press enter key.

       && sudo chmod +x install.sh \ 

10.7.4 Type && sudo ./install.sh \ and press enter key.

      && sudo ./install.sh \ 

10.7.5 Type && rm install.sh and press enter key.

      && rm install.sh

For Windows users

Download the binary file from the link and follow the prompt to install

https://github.com/thedevsaddam/docgen/releases

10.8 open the terminal and type the following and press enter

      docgen build -i postman_collection.json -o index.html

In the above command line -i specifies the source of the input file and -o specifies where the output file will be stored.

11. Integrate Africa’stalking SMS API: In the application, the integration with africa’stalking SMS API was done using the endpoint for forgotpassword in the auth.js controller.

This above code runs when request is made to the forgotpassword endpoint. The options object property to should be filled with required data.

This above code runs when request is made to the forgotpassword endpoint. The options object property to should be filled with required data.

12. Deploy the application to Heroku following the steps stated earlier for deploying applications on Heroku.

Conclusion

  • The above writing discussed steps on how to build applications using node https module, express, africa’stalking API, and MongoDB. Following the above steps and the references, similar applications, other kinds of applications can be built.

Write-up GitHub Repository Link

The Link to the GitHub repository for the write up can be found Here.

References

1 Shares:
You May Also Like