Quick RESTful API calculator using Node.js

NSerus
3 min readMar 29, 2021

REST Architecture

REST REpresentational State Transfer is an architecture model that uses the HTTP protocol for communication with the GET, POST, PUT and DELETE methods.

The best known representation methods to present a resource are JSON and XML. For more info click here.

Starting out…

After creating a directory for our project you need to…

  • Initialize the project.
npm init
  • Get the needed packages.
npm install --save hapi

Getting POSTMAN

Postman is a great tool when trying to dissect RESTful APIs for testing. It offers nice user interface that is able make HTML requests, without the hassle of writing a bunch of code just to test an API’s functionality.

This time we are going to use different methods other than get, that means that testing out the “browser” way it’s out of the question, we will need to use POSTMAN or another REST API Client.

You can get it on Manjaro Linux (using yay) by entering:

yay -S postman-bin

Creating the Node.js server

Create the index.js file:

touch index.js

And in that file write code to associate the hapi dependence, and initialization for the server:

//Framework hapi.js
const Hapi = require('hapi');

// logic port and machine
const host = 'localhost';
const port = 8000;

// Create Server
const server = Hapi.Server({
host: host,
port: port
});

// Initiate Server
const init = async () => {

await server.start();
console.log("Server up no porto: " + port);

}

//Initiate app
init();

Execute the app with the command (on the terminal) with:

$ node index.js

Now we can create a route to test out the POSTMAN capabilities. Just and the this code AFTER the init line:

server.route({
method: 'GET',
path: '/',
handler: function (request, h) {

var data = {
msg: 'API Calculadora'
};

return data;
}
});

Testing it out on postman, it should give:

Organization of the App (MVC)

Our app will follow the MVC design.

  • M — Model — Structures for data of the database.
  • V — View — What the users see.
  • C — Controller — Controls the interactions between everything.

There are also Routes that indicate which controller to use using the url/path.

Creating Routes:

$ mkdir routes 
$ cd routes
$ touch routes.js

On the index.js, before the init(), invoke the routes file:

//Framework hapi.js
const Hapi = require('hapi');

// logic port and machine
const host = 'localhost';
const port = 8000;

// Create Server
const server = Hapi.Server({
host: host,
port: port
});

// Initiate Server
const init = async () => {

await server.start();
console.log("Server up no porto: " + port);

}
//Define routes
require('./routes/routes')(server);
//Initiate App
init();

Route Files with the calculator operations

After the app being executed, we can easily test out all the operations using POSTMAN (or a web browser, although i wont be using browsers anymore).

Testing other Methods

You can think about methods like they are “another universe” for that path, where it has other code that you implement, of course you should follow good practice and make the methods do what they say (Post to post stuff and Get to get) but it’s really your decision.

Implementing another message on the testing path using POST should show how the methods work.

Just put this code on the Routes file to test it out:

Using POSTMAN, you can test the POST method, and will see that you get a different result:

--

--