Tutorial Node.js: Create REST API Calculator

Pedro Antunes
3 min readMar 24, 2021

--

If this is your first time using Node.js i sugguest you to check my previous tutorials How to make your first program in Node.js and How to create a server in node.js

The first step is to create a folder named “calculator”, the using the following commands we will change the directory in our node.js prompt:

cd calculator

Then we will initializxe our project using:

npm init

The next step is to create the necessary packages such as:

hapi — framework to create web apps.

Using the following command:

npm install — save hapi

The next step is to create our index.js file to do it we will use notepad++ and add the following code:

Then we will create another folder inside “calculator” folder, inside we will create a JavaScript file with the following code, whitour basic operations for our calculator work:

module.exports = function(server) {

//route About
server.route({
method: ‘GET’,
path: ‘/calculadora/about’,
handler: function (pedido){

var data = {
msg: ‘API Calculadora’
};

return data;
}
});

//route Soma
server.route({
method: ‘GET’,
path: ‘/calculadora/soma/{num1}/{num2}’,
handler: function (pedido) {

const num1 = parseInt(pedido.params.num1);
const num2 = parseInt(pedido.params.num2);

var data = {
resultado: num1 + num2
};

return data;
}
});

//route Subtração
server.route({
method: ‘GET’,
path: ‘/calculadora/sub/{num1}/{num2}’,
handler: function (pedido){

const num1 = parseInt(pedido.params.num1);
const num2 = parseInt(pedido.params.num2);

var data = {
resultado: num1 — num2
};

return data;
}
});
//route Multiplicação
server.route({
method: ‘GET’,
path: ‘/calculadora/multi/{num1}/{num2}’,
handler: function (pedido){

const num1 = parseInt(pedido.params.num1);
const num2 = parseInt(pedido.params.num2);

var data = {
resultado: num1 * num2
};

return data;
}
});

//route Divisão
server.route({
method: ‘GET’,
path: ‘/calculadora/div/{num1}/{num2}’,
handler: function (pedido){

const num1 = parseInt(pedido.params.num1);
const num2 = parseInt(pedido.params.num2);

var data = {
resultado: num1 / num2
};

return data;
}
});
}

To activate our server we will type the following command on node.js prompt (on the directory, where index.js is placed)

node index.js

For the next step we will use POSTMAN software (tou can download it HERE).

Lets type the following url to teste our about page:

http://localhost:8081/calculadora/about

And the output should look like this:

Output pag about

To test the sum we will type the following url:

http://localhost:8081/calculadora/soma/4/4

And the output should look like this:

Output sum

To test the substraction we will type the following url:

http://localhost:8081/calculadora/soma/4/4

And the output should look like this:

Output subtraction

To test the multiplication we will type the following url:

http://localhost:8081/calculadora/multi/4/4

And the output should look like this:

Output multiplication

--

--

Pedro Antunes
Pedro Antunes

No responses yet