Send Email In Node.JS In Hindi | NodeMailer Example
Express.js helmet Example | Enhance Security In Node.JS App
Difference Between SOAP AND REST API In Node - Express In Hindi
Hardware Boot Process क्या है | Computer Boot कैसे होता है ?
PHP Form validation | Validate User Input Data In PHP In Hindi
What is Router in Networking? and How router Works?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
REST APIs
अगर आप software development / design field में है तो definitely आपने Rest API word जरूर सुना होगा।
Well , Rest APIs क्या है और कैसे इन्हे implement करते हैं , इस topic में हम detail में समझेंगे।
●●●
REST (Representational State Transfer) एक API (Application Programming Interface) design paradigms हैं , जिन्हे web services बनाने और उनसे interact करने के लिए use किया जाता है।
यह HTTP protocol पर आधारित होता है। REST APIs HTTP methods (GET, POST, PUT, DELETE) का use करती है और data को JSON, XML, या दूसरे formats send करती है।
RESTful APIs client और server के बीच communication और data exchange के लिए use की जाती है। इन APIs का use अक्सर data को CRUD
(Create, Read, Update, Delete) operations करने के लिए होता है , लेकिन इन्हे आप अपनी need के according भी implement कर सकते हैं।
●●●
1. Statelessness : REST APIs stateless होती है , मतलब हर request एक दूसरे से संभाविक रूप से जुडी होती है। server को client की पिछली requests या states के bare में कुछ पता नहीं होता है। जो scalability और fault tolerance में help करती है।
2. URI (Uniform Resource Identifier) : हर resource के लिए एक unique URI
होता है , जिससे client resource को identify करता है और इसके according ही response मिलता है।
3. Security :
RESTful API के लिए security options ad-hoc होते हैं , मतलब आपको अलग - अलग security mechanisms का use करने की freedom रहती है। या कह सकते हैं आप need के according security mechanisms implement और apply कर सकते हैं।
4. Performance :
RESTful API में data का format अक्सर lightweight होता है जिससे network पर कम load होता है और better performance मिलती है।
5. Resource-Based Approach : APIs resources (जैसे objects या data) के रूप में represent करते हैं और उन resources पर CRUD operations allow करते हैं।
●●●
यहां Node.JS - ExpressJS का use करके एक simple RESTful API का example दिया गया है -
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
// define an array of objects for response as we are not using any database.
const books = [
{ id: 1, title: 'Book 1' },
{ id: 2, title: 'Book 2' },
{ id: 3, title: 'Book 3' },
];
// Get all books
app.get('/books', (req, res) => {
res.json(books);
});
// Get a specific book by ID
app.get('/books/:id', (req, res) => {
const bookId = parseInt(req.params.id);
const book = books.find((b) => b.id === bookId);
if (!book) {
res.status(404).json({ error: 'Book not found' });
} else {
res.json(book);
}
});
// Create a new book
app.post('/books', (req, res) => {
const newBook = req.body;
books.push(newBook);
res.status(201).json(newBook);
});
// Update a book by ID
app.put('/books/:id', (req, res) => {
const bookId = parseInt(req.params.id);
const updatedBook = req.body;
const index = books.findIndex((b) => b.id === bookId);
if (index === -1) {
res.status(404).json({ error: 'Book not found' });
} else {
books[index] = updatedBook;
res.json(updatedBook);
}
});
// Delete a book by ID
app.delete('/books/:id', (req, res) => {
const bookId = parseInt(req.params.id);
const index = books.findIndex((b) => b.id === bookId);
if (index === -1) {
res.status(404).json({ error: 'Book not found' });
} else {
books.splice(index, 1);
res.sendStatus(204);
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Example में RESTful API का server बनाया जो books
resources को represent करता है , और CRUD
operations support करता है।
Get, Post, Put, और Delete HTTP methods का use resources को access और manipulate करने के लिए किया गया है।
I Hope , आपको RESTFul APIs के बारे में अच्छे से समझ आया होगा।
●●●
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook