CRUD

CRUD stands for Create, Read, Update, and Delete. It represents the four basic operations that can be performed on data in a database or a persistent storage system.

CRUD operations are essential in managing data in applications. Each letter in CRUD corresponds to an action that allows for handling data:

Example in JavaScript (using a basic REST API):

// Importing required modules
const express = require('express');
const app = express();
app.use(express.json());

// In-memory data store (for demonstration purposes)
let items = [];

// Create - Add a new item
app.post('/items', (req, res) => {
    const newItem = req.body;
    items.push(newItem);
    res.status(201).send('Item created');
});

// Read - Get all items
app.get('/items', (req, res) => {
    res.json(items);
});

// Update - Modify an existing item
app.put('/items/:id', (req, res) => {
    const id = req.params.id;
    const updatedItem = req.body;
    items = items.map(item => item.id === id ? updatedItem : item);
    res.send('Item updated');
});

// Delete - Remove an item
app.delete('/items/:id', (req, res) => {
    const id = req.params.id;
    items = items.filter(item => item.id !== id);
    res.send('Item deleted');
});

// Starting the server
const port = 3000;
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

In this example: