TABLE OF CONTENTS
1. Introduction2. What is REST API? Need for REST API4. NodeJS5: Building REST API Here are the best practices for creating APIs7. Conclusion8. CloudThat 9. Frequently Asked Questions (FAQs). Introduction
This blog will explain the REST API and create an easy REST API using ExpressJS and NodeJS.
2. What is REST API?
REST stands for Representational state Transfer. It is an architectural style used to create web services. It uses HTTP protocol to make HTTP requests over the World Wide Web. RESTful web services use Uniform Resource Identifiers (URI), which can be text, JSON or XML. However, JSON is the most common.
REST APIs are stateless operations. This means that the current operation is independent of previous operations. REST architecture can create the following main functionalities: Create, Read (Update), Delete (CRUD). These operations can be accomplished using the most common HTTP methods: –
GET – Gives you read access to the Resource
POST – Create new resource
PATCH – Updates existing Resources
DELETE – Removes a resource
3. Need for REST API
REST API does not require any programming language or platform. It can be used with any programming language, and can use a variety syntaxes. It can also be built on any platform such as Windows, Linux, MAC OS, and so forth.
The REST API allows the client and server to be used independently in different development projects. REST API allows the client and server to be developed independently.
REST APIs can be scaled because of client-server separation.
4. NodeJS is used to build REST APIs
This tutorial will show you how to create a simple CRUD API with ExpressJS and NodeJS. First, we will need to install ExpressJS and NodeJS in our application. This object will contain our data and not use any database.
I will use VS Code Editor in this application. You can choose between Code Editor or IDE.
Step 1: Create a project directory. Open the terminal and navigate to the project directory. Give the command npminit -y to your terminal. It will generate a boilerplate to our node application.
Step 2: Next we will install Express.JS by using the command npm i Express in our application.
Step 3: Next, we will install nodemon. This restarts the NodeJS Server whenever there is a code change. Nodemon detects any changes and restarts your server for you. Terminal write npmi -g nodemon
Our package.json looks something like this.
Step 4: Now we will create a basic expressJS server to run our application on port 33000. We will create a file called “app.js”.
Step 5: We will first create our “//” route, which will run as soon as our application starts.
We will create a book object because we are using an object in our database.
Step 6: Now we will perform a READ operation. This will retrieve all books in our object. We will use the “GET” method to perform a read operation.
We will pass the id in our route to check if it is in our object.
Step 7: Now, we will perform the CREATE operation. This operation will create an object entry. We will use the “POST” operation to create an operation.
Step 8: Now, we will perform the UPDATE operation. Because it allows us to only send the fields we wish to change, we will use the “PATCH” method. It will go through /books/.id to send the fields that we want to modify.
Step 9: Finally, we will