Table of contents
R-REpresentational S-State T-Transfer
For some story, purpose let us understand this by a story that we are in a hotel and we are ordering some food to eat consider that we are the client and the hotel kitchen where they make things is the server and the man who is going to give us the menu is out API, In simple when we are building an API we are building the menu to server.
**What is restful **--> is an Architectural style
What is architectural --> Architecture is the art and technique of designing and building, distinguished from the skills associated with construction. It is both the process and the product of sketching, conceiving, planning, designing, and constructing buildings or other structures.
Architecture simply style REST is an architectural style to design the API's also we have SOAP, GRAPH QL, FALCOR, but the gold standard for the web is REST
**Idea Behind the creators of this RESTapi ** The idea behind creating this is in simple terms that we can tell that consider that we have so many hotels in our local and each and every hotel has different styles for their building some have for example restroom on the first floor and some have them in 3rd floor or ground floor so we have to ask for somebody to guide imagine that we have them all the building same floor in the same position then we will never get into a situation where we try to find out so essentially the Ph.D. proposal said the same thing that if every web API was built using the same common guiding principles then it would be so easy for everybody to work efficiently
So, how exactly do we make our API restful there are a lot of rules that an API has to follow and like rules, we follow in school college but two Most things are using
--->Http Request Verbs --->Use Specific Patterns of Routes/endpoints URL/s
HTTP Request Verbs are -->GET, POST, PUT, PATCH, DELETE
Use Specific Patterns of Routes/endpoints URL/s --> For example consider we are going to any county to visit someplace, for example, we go to china to visit the great china wall and India to visit the taj mahal and the USA to visit Elon Must😂 ok, let us consider this as routes that have/china-wall and /Tajmahal and /Elon, we might have in internet world google.com/china-wall like this for all so on an these are routes there are used using Express and Node.js in order to our server to be RESTful we have to have a specific pattern of endpoints and routes, for example, we have Wikipedia API See the below picture
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const {
application
} = require("express");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
mongoose.connect("mongodb+srv://admin-karan:KaranDatabase@cluster0.2xfnvkx.mongodb.net/wikiDB")
const articalSchema = {
title: String,
content: String
}
const Article = mongoose.model("Article", articalSchema)
//////////////////////////////Request targetting all specifig articcal///////////////
app.route("/articles")
.get(function (req, res) {
Article.find(function (err, foundArticles) {
res.send(foundArticles)
})
})
.post(function (req, res) {
console.log(req.body.title)
console.log(req.body.content)
const newArticle = new Article({
title: req.body.title,
content: req.body.content
})
newArticle.save(function (err) {
if (!err) {
res.send("succesfull Sent by karan")
} else {
res.send(err)
}
})
})
.delete(function (req, res) {
Article.deleteMany(function (err) {
if (!err) {
res.send("succesfully Deleted")
} else {
res.send(err)
}
})
})
//////////////////////////////Request targetting a specifig articcal///////////////
app.route("/articles/:articleTitle")
.get(function(req,res){
Article.findOne({title:req.params.articleTitle},function(err,foundArticle){
if (foundArticle) {
res.send(foundArticle)
}else{
res.send("Nor Articles Matching The Title was found!")
}
})
})
.put(function(req,res){
Article.updateOne(
{title:req.params.articleTitle},
{title:req.body.title,content:req.body.content},
{overwrite:true},
function(err){
if (!err) {
res.send("Succesfully updated the fields")
}else{
res.send("Someting went wrong")
}
}
)
})
.patch(function(req,res){
Article.updateOne(
{title:req.params.articleTitle},
{$set:req.body},
function(err){
if (!err) {
res.send("Patch succesfull")
}else{
res.send("patch Unsuccessfull")
}
}
)
})
.delete(function(req,res){
Article.deleteOne(
{title:req.params.articleTitle},function(err){
if (!err) {
res.send("Sucessfully Deleted the artical")
}else{
res.send("No Articles Matching The Title was found!")
}
}
)
})
//server log
app.listen(3000, function () {
console.log("Server started on port 3000");
});
Further studies for these RESTful APIs %[youtube.com/watch?v=SLwpqD8n3d0&ab_chan..
Thank you for reading!