node.js tutorial: API calculator using Swagger

Afonso Antunes
2 min readApr 26, 2021

--

Hi people!!

In this tutorial, you will learn how to make a calculator API with elementary arithmetic operations, using Swagger.

Swagger is an interface description language for describing RESTful APIs expressed using JSON and is used in conjunction with a set of Open Source software tools to design, build, document and use RESTful web services.

So let’s start the tutorial!!

1st step: Open the command line and use the following commands to create the project.

mkdir calculatorSwagger
cd calculatorSwagger
npm init

After that, we will indicate the requested parameters.

2nd step: Install the necessary dependencies (express and swagger-ui)

npm install express
npm install swagger-ui-express --save

3rd step: Create index.js and swagger.json files

touch index.js
touch swagger.json

4th step: Place the following code in the index.js file

5th step: Place the following code in the swagger.json file where we are going to create the elementary arithmetic operations (sum, subtraction, multiplication and division)

Last step: Run the project

node index.js

Open the address http://localhost:8888/calculator/

And test the operations

Sum operation
Subtraction operation
Multiplication operation
Division operation

And done!!

--

--