node.js: Rest API (calculator)

  • Create — INSERT
  • Read — SELECT
  • Update — UPDATE
  • Delete — DELETE

1st step: Create a directory for the project

mkdir calculator
cd calculator

2nd step: Start the project

npm init

3rd step: Installing required packages

npm install — save hapi

4th step: Create the server (with node.js)

touch index.js

5th step: Put the following code

//Framework hapi.jsconst Hapi = require('hapi');// Machine and logical portconst host = 'localhost';const port = 8000;// Create the serverconst server = Hapi.Server({host: host,port: port});// Start the serverconst init = async () => {await server.start();console.log("Server up no porto: " + port);}// App launchinit();

6th step: Test the app running the following command…

node index.js
The result, at this moment

7th step: Creating an about app after the init() function

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

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

return data;
}
});

8th step: Test the app again (with the command in the 6th step) and put this link in your browser:

http://localhost:8000/calculator/about

App organization (MVC)

  • M : for Model (code for our database model);
  • V : for View (layout);
  • C : for controllers (logical part of the app).

9th step: Create routes

mkdir routes
cd routes
touch routes.js
// Define routersrequire('./routes/routes')(server);

10th step and most important: enter the basic operations of the calculator

module.exports = function(server) {
//route Somaserver.route({method: 'GET',path: '/calculator/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çãoserver.route({method: 'GET',path: '/calculator/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çãoserver.route({method: 'GET',path: '/calculator/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ãoserver.route({method: 'GET',path: '/calculator/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;}});
}

--

--

--

Finalist student in Computer Engineering

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

HackThisSite JavaScript Mission 4

ES6 Map Object

JSON store as a service: jsonbin

HackerRank Challenge: Sherlock and Anagrams

Creating Foreign JMS Server configurations through WLST script in Weblogic server

weblogic-foreign-JMS-server

What’s New in React-Native 0.63

What is Node.JS ?

Simple guide to use RabbitMQ with NODEJS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afonso Antunes

Afonso Antunes

Finalist student in Computer Engineering

More from Medium

Uses of JavaScript and MongoDB

Testing JavaScript Application with Jest

How To Test a NodeJS Module using Mocha and Assert

Node Js For Web Development