TGViewer
Web Development Web Development @webdevcoursefree ยท 79.6K subscribers
Post #3574 4.62K
Web Development Roadmap

๐ŸŒ REST APIs & Routing in Express

Now you move from basic server โ†’ real backend API structure.

If you understand this topic properly, you can build production-level APIs.

๐Ÿง  What is a REST API?

REST = Representational State Transfer

Simple meaning:
๐Ÿ‘‰ Backend exposes URLs
๐Ÿ‘‰ Frontend sends HTTP requests
๐Ÿ‘‰ Backend returns data (usually JSON)

๐Ÿ”ฅ REST API Structure

REST follows resources-based URLs.

Example resource: users

Instead of:
- /addUser
- /deleteUser

REST style:
- POST /users
- PUT /users/:id
- DELETE /users/:id

๐Ÿ“Œ HTTP Methods in REST

- GET -> Read data
- POST -> Create data
- PUT -> Update data
- DELETE -> Remove data

These map directly to CRUD.

๐Ÿงฉ Basic REST API Example

Step 1: Setup Express

const express = require("express");
const app = express();

app.use(express.json()); // middleware for JSON

let users = [
{ id: 1, name: "Amit" },
{ id: 2, name: "Rahul" }
];


๐Ÿ” 1๏ธโƒฃ GET โ€“ Fetch all users

app.get("/users", (req, res) => {
res.json(users);
});


โž• 2๏ธโƒฃ POST โ€“ Add new user

app.post("/users", (req, res) => {
const newUser = {
id: users.length + 1,
name: req.body.name
};

users.push(newUser);
res.json(newUser);
});


โœ๏ธ 3๏ธโƒฃ PUT โ€“ Update user

app.put("/users/:id", (req, res) => {
const id = parseInt(req.params.id);
const user = users.find(u => u.id === id);

if (!user) {
return res.status(404).json({ message: "User not found" });
}

user.name = req.body.name;
res.json(user);
});


โŒ 4๏ธโƒฃ DELETE โ€“ Remove user

app.delete("/users/:id", (req, res) => {
const id = parseInt(req.params.id);
users = users.filter(u => u.id !== id);
res.json({ message: "User deleted" });
});


โ–ถ๏ธ Start Server

app.listen(3000, () => {
console.log("Server running on port 3000");
});


๐Ÿง  What is Routing?

Routing means:
๐Ÿ‘‰ Matching URL
๐Ÿ‘‰ Matching HTTP method
๐Ÿ‘‰ Running correct function

Example:
- GET /users โ†’ fetch users
- POST /users โ†’ create user

๐Ÿ“ Better Folder Structure (Real Projects)

project/
โ”œโ”€โ”€ routes/
โ”‚ โ””โ”€โ”€ userRoutes.js
โ”œโ”€โ”€ controllers/
โ”œโ”€โ”€ server.js

Separation of concerns = scalable backend.

โš ๏ธ Common Beginner Mistakes

- Not using express.json()
- Not parsing req.params correctly
- Not sending status codes
- Not handling missing data

๐Ÿงช Mini Practice Task

- Create REST API for products
- GET /products
- POST /products
- PUT /products/:id
- DELETE /products/:id

โžก๏ธ Double Tap โ™ฅ๏ธ For More
  • โค 15
  • ๐Ÿ”ฅ 2
More from @webdevcoursefree
  1. Oct 14, 2026๐ŸŒ Web Development Roadmap โ€” Part 17 HTML Images โ€” <img> Tag Images make webpages more visโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026For example: Go to Profile โ†’ <a> Submit Form โ†’ <button> Open Article โ†’ <a> Delete Accountโ€ฆ
  4. Oct 12, 2026๐ŸŒ Web Development Roadmap โ€” Part 16 HTML Links โ€” Tag Links are one of the most importantโ€ฆ
  5. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  6. Oct 9, 2026Post #4079
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’