REST stands for Representational State Transfer. REST is a web standards-based architecture and uses HTTP Protocol.
REST was first introduced by Roy Fielding in 2000.
HTTP methods
The following four HTTP methods are commonly used in REST-based architecture more they are some of them with their endpoints.
GET
/listusers
− This is used to provide read-only access to a resource.PUT
/addUser
− This is used to create a new resource.DELETE
/deleteUser
− This is used to remove a resource.POST
/update
− This is used to update an existing resource or create a new resource.
Why should you use Node.js and Express to build your REST API?
The ability to use a single language (JavaScript) for both client-side and server-side development.
Fast and powerful performance, owing to the ability to run multiple requests in parallel.
Middleware and routing capabilities are built into Express to make API development quick and easy.
A large, active community of developers contributing to the ecosystem.
RESTful Web Services
A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications are written in various programming languages and run on various platforms.
Web services based on REST Architecture are known as RESTful web services.
Let's Make API's 🤗
Step 1
Create a JSON file for the data which you want to serve on the page through API we are using Get API
to show the data.
// user.json file
{
"user1" : {
"name" : "sumit",
"password" : "password1",
"profession" : "engineer",
"id": 1
},
"user2" : {
"name" : "saurabh",
"password" : "password2",
"profession" : "engineer",
"id": 2
}
}
GET 🤓
const express = require('express');
const app = express();
const fs = require("fs");
app.use(express.json());
const port = 3000;
app.get('/listUsers', function (req, res) {
fs.readFile( __dirname + "/" + "user.json", 'utf8', function (err, data) {
console.log( data );
res.end( data );
});
})
app.listen(port,()=>{
console.log(`server is Ruming on port ${port}`);
})
Output
PUT 🫡
const express = require('express');
const app = express();
const fs = require("fs");
const adduser = {
"user3":{
"name":"tom",
"password":"password4",
"profession":"teacher",
"id":4
}
}
app.use(express.json());
const port = 3000;
app.post('/addUser', function (req, res) {
fs.readFile( __dirname + "/" + "user.json", 'utf8', function (err, data) {
data = JSON.parse(data);
data["user3"] = adduser["user3"]
console.log( data );
res.end(JSON.stringify(data));
});
})
app.listen(port,()=>{
console.log(`server is Ruming on port ${port}`);
})
Output
DELETE 😬
const express = require('express');
const app = express();
const fs = require("fs");
const adduser = {
"user3":{
"name":"tom",
"password":"password4",
"profession":"teacher",
"id":4
}
}
const id = 1;
app.use(express.json());
const port = 3000;
app.delete('/deleteUser', function (req, res) {
fs.readFile( __dirname + "/" + "user.json", 'utf8', function (err, data) {
data = JSON.parse(data);
data["user"] = adduser["user3"]
// delete user NO.1
delete data["user"+1];
console.log( data );
res.end(JSON.stringify(data));
});
})
app.listen(port,()=>{
console.log(`server is Ruming on port ${port}`);
})
Output
Thank you for reading my content. Be sure to follow and comment on what you want me to write about next
🤓