![]() |
VOOZH | about |
A REST API or Representational State Transfer API, is a set of rules that allows different applications to communicate with each other over the internet. It enables clients, such as web browsers or mobile apps, to send requests to a server and receive data or perform actions. REST APIs are widely used because they are simple, flexible, and work with standard HTTP methods.
REST APIs follow a set of architectural principles that make them scalable, simple, and widely usable across the web. Fundamental principles that define their architecture and functionality:
In RESTful APIs, HTTP methods define the actions to be performed on resources. Each HTTP method corresponds to a specific operation in the CRUD (Create, Read, Update, Delete) cycle. Here’s an explanation of the commonly used HTTP methods:
It is used to retrieve an existing resource from the server. The server responds with the resource's data, often in JSON or XML format. It is used to read the data.
GET http://example.com/api/users
GET http://example.com/api/users/123
It is used to add new resource on the server. It sends data to the server as part of the request body, typically in JSON or XML format.
POST /api/users
{
"name": "John Doe",
"email": "geeks.doe@example.com"
}
It is used to update an existing resource by sending a complete representation of the resource to the server. The server replaces the current data with the new data provided in the request body.
PUT /users/1
{
"name": "John Doe",
"email": "john.doe@example.com"
}
It is used to modify specific fields of an existing resource. Instead of sending the entire resource, the client sends only the data that needs to be changed, making updates more efficient when only a few attributes require modification.
PATCH /users/1
{
"email": "newemail@example.com"
}
This method is used to delete a resource from the server. Once executed successfully, it usually returns a status code indicating the deletion has been completed, such as 200 OK or 204 No Content. It is used to delete the data.
DELETE http://example.com/api/users/123Following HTTP status codes indicate the result of the request:
You can create a simple REST API in using the built-in http module or Express.js framework, simplifying routing and middleware handling.
Create a new Node.js project, initialize package.json, and install Express using the following commands:
mkdir node-app
cd node-app
npm init -y
npm install express
Creating a REST API in NodeJS using Express
Run the server
node app.jsOutput
Open the http://localhost:3000 on the postman to check the API is working properly or not.
In this example