Create RESTful APIs

🔍 What are RESTful APIs?

RESTful APIs are APIs that follow the REST (Representational State Transfer) architectural style, using HTTP methods like GET, POST, PUT, and DELETE.

They are designed to perform CRUD (Create, Read, Update, Delete) operations on resources over the internet.

🎯 Why Use RESTful APIs?

🛠️ How to Create REST APIs in Express

Use Node.js with the Express framework to define routes and handle client requests.

// Basic RESTful routes in Express
const express = require('express');
const app = express();
app.use(express.json());

// GET - Read
app.get('/api/products', (req, res) => {
  res.send('All Products');
});

// POST - Create
app.post('/api/products', (req, res) => {
  res.send('Product Created');
});

// PUT - Update
app.put('/api/products/:id', (req, res) => {
  res.send('Product Updated');
});

// DELETE - Remove
app.delete('/api/products/:id', (req, res) => {
  res.send('Product Deleted');
});

app.listen(5000, () => console.log('Server running on port 5000'));

📅 When to Use RESTful APIs?

Use them when your frontend (React, Vue, mobile app, etc.) needs to exchange data with your backend server. For example: