[Swagger 101] SwaggerCalculatorAPI

As a Distributed Systems 2020–21 Class project, in this article we will create a very simple API called SwaggerCalculatorAPI using the Nodejs and Swagger that will allow us to make a simple math operation with a GET request where we will use the Swagger yaml code to create the UI of our API. This API can be later improved as anyone desires.

[ Getting Started ]

Having the Visual Studio Code installed on a Windows 7 machine, we will start the VSCode and on the left of the window, on the explorer, we will create a new folder named SwaggerCalculatorApi.

Right-click on it, and create a New File named index.js that later we will work on it.

Right-click again on the folder explorer item and click Open in Integrated Terminal (and choose as New Command Prompt in the dropdown menu).

Navigate to the SwaggerCalculatorApi folder on the terminal and create a new project with it by using the following command:

npm init

For now we will only insert the author, but you can fill all the fields when asked or change later on the file package.json that will be created after this step is done. This one will keep info and dependencies about the project.

Install the following modules:

npm install express cors swagger-jsdoc@6.0.0 swagger-ui-express

[ index.js ]

On the index.js file put the following code:


On the project folder SwaggerCalculatorApi, create a new directory and inside create a calculator.js file, and add the following code:


Create a new file named controller.js inside the project folder and paste the following code:

Now, if we execute node index.js on the terminal in the project path, we will get something like the following picture:

And if we Try it out it and input some values like 3 + 5, the response body will give a output with the end result:

As we can see, our API is working as intended.

Hope you liked this small article, best regards, Ricardo Costa (Richards).

This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.

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