Tutorial Node.js: REST API (Calculator)

CRUD:

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

Begin with the usual, create a repository and a .json package:

For this one we will need to use hapi as a framework, that is used to create apps web.

For the index.js use:
//Framework hapi.js
const Hapi = require(‘hapi’);

// Máquina e Porto Lógico
const host = ‘localhost’;
const port = 3000;

// Criação do Servidor
const server = Hapi.Server({
host: host,
port: port
});

// Iniciar servidor
const init = async () => {

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

}

//Inicialização da App
init();

Everthing is set to start so: node index.js

Used Postman to test the app

Now we need to create routes for the app to be able to function as wanted.

Lets try this:
mkdir routes
cd routes
create file routes.js

Now back to the index.js file lets set the routes:

To update the routes file lets use the following code:
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; }

});

}

--

--

--

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

Recommended from Medium

A smart way to display “No items found” on a list

React-native monorepo with shared code and hooks

Firebase serve with TypeScript build Automatically.

TypeScript-based caching decorators

Sorry, but Functors are not a means to map over an Array

UNIQUE token usecases

Webpack: the Easiest Example

Testing (Camunda)-DMN Tables automatically

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
João Saraiva

João Saraiva

More from Medium

Authorization vs Authentication

Authentication for Dummies

What is Authorization (with examples)

Node.js and Sequelize ORM — Maintain seed data execution history