DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 42 Of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 42 — marking exactly six full weeks of unbroken daily consistency on my roadmap to full-stack MERN engineering! Yesterday, I mastered Express middleware. Today, I advanced deeper into Prashant Sir's (Complete Coding) backend masterclass to implement true industry architecture: REST API Design and Dynamic Routing.

Instead of creating arbitrary, messy route configurations, today I learned how to structure clean, predictable data endpoints following international software standards.


🧠 Key Learnings From Node.js Lecture 10 (REST APIs & Dynamic Parameters)

REST (Representational State Transfer) is the golden standard for web service communication. Here is the technical breakdown of how I engineered my backend channels today:

1. The Standard REST Architectural Rules

I learned that an endpoint URL should represent a resource (noun) rather than an action (verb).

  • Incorrect Pattern: /getUsers or /createNewUser
  • Correct REST Pattern: GET /api/users (To read) or POST /api/users (To create)

2. Dynamic Routing Path Extraction (req.params)

When an application needs to pull a specific record out of thousands, creating individual static endpoints is impossible. Express solves this using dynamic path parameter definitions (/:id). I learned how to fetch this variable out of the network wire cleanly using req.params:


javascript
const express = require("express");
const app = express();
const users = require("./MOCK_DATA.json"); // Simulating a local JSON store

// GET a single user dynamically by their unique ID string variable
app.get("/api/users/:id", (req, res) => {
    const id = Number(req.params.id); // Grabbing path parameters cleanly
    const user = users.find((u) => u.id === id);

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

app.listen(8000);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)