How to implement REST API?

Subashinipiumali
5 min readMar 22, 2021

I explained the Rest API concept in the previous article. In this article, I’ll demonstrate how to implement the REST API using Node.js

First, install node.js.

you can clearly see that I have the version 14 point 16. Next, you have to basically call the NPM package to initialize the NPM modules into your system. To do that you will type in Npm init.

Once you type npm init. They will be asked to enter the details for your project. For example, package name to be a demo. you cannot enter capital letters. so I’ll just type in demo and then version it be 1.0 point only. Then you have to enter a description of your project. In this figure, you can see, I mentioned the creation of the rest API as my description. After that entry point this is the main thing that you basically have to enter. That is the entry point file. you can put any name for the entry point file. I used the name script.js. After that you’ll see you have test command I’ll just click on enter get a repository to enter keywords enter. Then type my name as author.

Next, it’ll ask you for confirmation. you define till now is basically the metadata for your project. Move your folder and you can see the package file has been created.

Next go back to your command prompt and you have to next install express. To install express you have to mention npm i and then mention the library name that is express.

Express is basically a web framework that can be used along with node.js and it allowed you to create a restful API with the help of helper methods middle layers to configure your application.

And also you have to install Joi. you can use “npm i joi” command to install.

Joi is used to validating your information whether it’s the right format or not. so all the time you don’t have to validate your server you can just directly this library and this library will validate the information for you.

And also you have to install Joi. you can use npm i joi to install.

Joi is used to validating your information whether it’s the right format or not. so all the time you don’t have to validate your server you can just directly this library and this library will validate the information for you.
Now, you have to install nodemon by using “npm i nodemon” command. Nodemon is basically used to keep a watch on all the files with any type of extension present in the folder. Also with nodemon you don’t have to restart the node.js server each time any changes are made. If you don’t use nodemon then you have to restart the server anytime you make changes so with the help of nodemon you don’t have to do that automatically nodmeon will implicitly detect the changes and then restart the server.

Next, you have to create script.js file that you mentioned in your package store JSON file.

And put following code to your script.js file.

To view the output of the application first you have to run your application. comman is the ‘node script.js’

I have used postman is basically a chrome plugin which is used to send the request to the servers. In postman application, you have to mention the port which is embedded in your script.js (localhost:8080/).

Let’s understand how we are sending information to the client and how the server is sending us back the response.

Start with the GET method and see the response. Display the Message when URL consist of ‘/’

Display the list of customers when URL consist of api/customers

Display the customer data based on given id.

POST method and output of the postman application. Inside the post method, input value will be validate using the function of validateCustomer.

According to the Validate function, the input string must have a minimum of three characters.

PUT method and response.

DELETE method and response.

the response after deleting the id of 05.

Conclusion

In this article, you learned how to implement REST API with the express library and how to send the request to the server using built rest API.

Thank you…

--

--