L9. ๐Ÿ› ๏ธ Node.js + Express โ€“ CRUD Operations

// CRUD OPERATIONS ๐Ÿงฉ
// C = Create | R = Read | U = Update | D = Delete

// Creating server using Express โš™๏ธ
import express from 'express';

const app = express(); // express app instance

// C = Create => POST method (e.g., posting pic on Instagram ๐Ÿ“ธ)
// R = Read   => GET method  (e.g., seeing posts on Instagram ๐Ÿ‘€)
// U = Update => PUT method  (e.g., editing a post โœ๏ธ)
// D = Delete => DELETE method (e.g., deleting a post โŒ)

// To handle each operation, we use specific HTTP methods ๐Ÿ”

// Read data (GET request) ๐Ÿ“ฅ
app.get('/get', (req, res) => {
    res.send("GET request");
});

// Create data (POST request) โž•
app.post('/post', (req, res) => {
    res.send("POST request");
});

// You can later add PUT and DELETE here for full CRUD

// Run the server on port 2000 ๐Ÿšช
const port = 2000;
app.listen(port, () => console.log(`Server is running on port ${port}`));

โœจ Explanation

  • Express.js makes it easier to handle HTTP methods like GET, POST, PUT, DELETE.

  • app.get() handles reading data (like viewing posts).

  • app.post() handles creating data (like uploading a photo).

  • You can add app.put() and app.delete() for full CRUD.

  • CRUD is the basic of any backend system and is used in almost every app.

0
Subscribe to my newsletter

Read articles from Sahana S Acharya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sahana S Acharya
Sahana S Acharya