DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 36 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 36 of my journey to master the MERN stack! Yesterday, I spun up my very first local web server. Today, I advanced into Prashant Sir's (Complete Coding) backend sequence to solve a major problem: How do we serve different content based on what the user types in the browser address bar?

Today was a deep dive into Backend Routing and capturing structural request parameters natively.


🧠 Key Learnings From Node.js Lecture 4 (Routing & Metadata)

When building an application layout, the backend acts as a traffic controller. Here is the mechanical breakdown of what I mastered today:

1. Navigating via req.url

I learned that the incoming Request object (req) holds the exact path the user is trying to access inside the req.url property. By deploying structural logical conditions, we can route users to different operational endpoints cleanly:


javascript
const server = http.createServer((req, res) => {
    if (req.url === "/") {
        res.end("Welcome to the Homepage!");
    } else if (req.url === "/about") {
        res.end("This is the About Page detailing our stack.");
    } else {
        res.writeHead(404, { "Content-Type": "text/plain" });
        res.end("404: Page Not Found!");
    }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)