Making a calculator with Node.js

The following article was written for college purposes.

What is Node.js?

Besides what most people think Node isn’t a programming language, it’s actually a Javascript runtime environment that executes JavaScript code outside a web browser.

It’s (mostly) developed in the back-end to create a web page with dynamic content.

What is MVC?

The Model View Controller is a design that divides the application into three main logical components, and we will be using it in this app.

The model is the central component of the pattern. It is the application’s dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.

The view represents any information such as a chart, diagram or table. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.

The controller accepts input and converts it to commands for the model or view.

#1 Install

Install Node on your machine, go to https://nodejs.org and download the last stable version. In case you are doing this in Linux the following two commands are for you:

sudo apt install nodejs

After installing nodejs install NPM (Node Package Manager).

sudo apt install npm

Check if the installation went ok:

npm -v
node -v

#2 Generate

Type the following command inside the directory you want to work in:

npm init

This will generate a package.json that contains information about the application. Press ENTER in case you don’t want to fill the information.

#3 Modules

In this case we will just need to install one module, which is hapi — a simple and powerfull framework to create web applications.

npm install hapi --save

#4 Server

Now let’s start our server. Your index.js, inside the dir calculator, should look like this. The file is pretty to understand.

  • First we call the module;
  • Then we set the address and the port that we want the to run on;
  • After both host and port are set, we create the server;
  • Now the server is able to start. The console.log helps you to make sure that the server has started and in which port.
  • In this case I wanted to show a message in /calculator/about so I created a simple route to print “API calculator @bernardosr1” ;
  • And finally start the app.
//Hapi framework
const Hapi = require('hapi');

//Address and port
const host = 'localhost';
const port = 3000;

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

//Start server
const init = async () => {
await server.start();
console.log("Server up! Port: " + port);
}
//About
server.route({
method: 'GET',
path: '/calculator/about',
handler: function (request, h) {

var data = {
message: 'API calculator @bernardosr1'
};

return data;
}
});
//Start App
init();

Then go to the terminal and type:

node index.js

After the server is up , you can go to your browser and check if everything is ok.

http://localhost:3000/calculator/about

#5 Routes

Routes are used to point to the controller that the user should use. This routes are based on the requested url.

Let’s create a folder named routes and then a routes.js.

As you might have noticed that in the index.js we have already created a route for the about.

But let’s organize our code and insert every route in route.js. Like this:

routes.js
index.js

Now you can create the routes for sum, subtraction, division and multiplication.

Right after about route:

//route - sum
server.route({
method: 'GET',

path: '/calculator/sum/{num1}+{num2}',
handler: function (request, h) {

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

var data = {
answer: num1 + num2
};

return data;
}
});
//route - subtraction
server.route({
method: 'GET',

path: '/calculadora/sub/{num1}-{num2}',
handler: function (request, h) {

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

var data = {
resposta: num1 - num2
};

return data;
}
});
//route - multiplication
server.route({
method: 'GET',

path: '/calculadora/multi/{num1}*{num2}',
handler: function (request, h) {

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

var data = {
resposta: num1 * num2
};

return data;
}
});
//route - division
server.route({
method: 'GET',

path: '/calculadora/div/{num1}/{num2}',
handler: function (request, h) {

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

var data = {
resposta: num1 / num2
};

return data;
}
});
}

#6 Results

Run your index.js and check the results!

Cybersecuity student at Guarda, Portugal.