Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀

Aaj ke time me agar backend development seekhni hai to REST API banana aana zaroori hai. REST API ka matlab simple hai – server aur client ke beech data ka exchange. Agar tum ek website ya mobile app bana rahe ho jo data store karti hai (like users, pr…


This content originally appeared on DEV Community and was authored by Pravin Jadhav

Aaj ke time me agar backend development seekhni hai to REST API banana aana zaroori hai. REST API ka matlab simple hai – server aur client ke beech data ka exchange. Agar tum ek website ya mobile app bana rahe ho jo data store karti hai (like users, products, posts, etc.), to API ka use hota hai.

Is blog me hum Express.js ka use karke ek simple REST API banayenge jisme CRUD operations honge – Create, Read, Update, Delete. Chalo bina time waste kiye shuru karte hain! 🔥

1️⃣ Pehle Express.js Setup Karo

Sabse pehle Node.js aur Express.js ka setup karna padega.

Step 1: Node.js Install Karo

Agar tumhare system me Node.js nahi hai to Node.js ki official website se install kar lo.

Node.js ek runtime environment hai jo JavaScript code ko run karne ke liye use hota hai. Iske bina Express.js kaam nahi karega.

Step 2: Naya Project Banaao

Terminal ya CMD me jaake yeh likho:

mkdir myapi
cd myapi
npm init -y

Yeh command ek package.json file banayegi jo project ke dependencies ko track karegi.

Step 3: Express Install Karo

npm install express

Isse Express.js install ho jayega jo hum API banane ke liye use karenge.

Ab humari basic setup ho gayi hai. Ab API ka code likhna shuru karte hain.

2️⃣ Simple REST API Likho

Ek naya file server.js banao aur isme yeh code likho:

const express = require('express');
const app = express();
app.use(express.json()); // JSON data handle karne ke liye

let users = [  // Dummy data
    { id: 1, name: "Pravin" },
    { id: 2, name: "Rahul" }
];

// ✅ GET - Sare users ka data bhejna
app.get('/users', (req, res) => {
    res.json(users);
});

// ✅ POST - Naya user add karna
app.post('/users', (req, res) => {
    let newUser = { id: users.length + 1, name: req.body.name };
    users.push(newUser);
    res.json(newUser);
});

// ✅ PUT - User ka naam update karna
app.put('/users/:id', (req, res) => {
    let user = users.find(u => u.id == req.params.id);
    if (!user) return res.status(404).send("User not found");
    user.name = req.body.name;
    res.json(user);
});

// ✅ DELETE - User hataana
app.delete('/users/:id', (req, res) => {
    users = users.filter(u => u.id != req.params.id);
    res.send("User deleted");
});

// Server start karo
app.listen(3000, () => console.log("REST API running on http://localhost:3000"));

Code Ka Breakdown

  1. express.json(): Yeh middleware request body me JSON data ko parse karta hai.
  2. GET Route: /users URL se users ka data return karta hai.
  3. POST Route: Naya user add karta hai jo request body me diya gaya ho.
  4. PUT Route: Existing user ka naam update karta hai.
  5. DELETE Route: User ko list se remove karta hai.
  6. app.listen(3000): Server ko port 3000 par start karta hai.

3️⃣ API Test Kaise Kare?

Ab server start karne ke liye terminal me likho:

node server.js

Agar tumhe nodemon use karna hai (taaki baar baar server restart na karna pade), to install karke yeh run karo:

npm install -g nodemon
nodemon server.js

Ab browser ya Postman se API ko test karo:

Method URL Kaam
GET http://localhost:3000/users Sare users ka data milega
POST http://localhost:3000/users Naya user add hoga (Body me JSON bhejna hoga)
PUT http://localhost:3000/users/1 User ka naam update hoga
DELETE http://localhost:3000/users/1 User delete hoga

Postman ya Thunder Client (VS Code extension) ka use karke JSON data bhejo aur test karo.


This content originally appeared on DEV Community and was authored by Pravin Jadhav


Print Share Comment Cite Upload Translate Updates
APA

Pravin Jadhav | Sciencx (2025-02-06T15:23:04+00:00) Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀. Retrieved from https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/

MLA
" » Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀." Pravin Jadhav | Sciencx - Thursday February 6, 2025, https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/
HARVARD
Pravin Jadhav | Sciencx Thursday February 6, 2025 » Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀., viewed ,<https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/>
VANCOUVER
Pravin Jadhav | Sciencx - » Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/
CHICAGO
" » Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀." Pravin Jadhav | Sciencx - Accessed . https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/
IEEE
" » Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀." Pravin Jadhav | Sciencx [Online]. Available: https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» Express.js REST API: Ekdum Simple Tarike Se Samjho! 🚀 | Pravin Jadhav | Sciencx | https://www.scien.cx/2025/02/06/express-js-rest-api-ekdum-simple-tarike-se-samjho-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.