Hello Dev Community! 👋
It is officially Day 37 of my continuous streak toward mastering the MERN stack! Yesterday, I configured clean structural routing to map pages like /about or /contact. Today, I advanced further into Prashant Sir's (Complete Coding) backend roadmap to tackle an essential data communication concept: URL Parsing and Query Parameters.
When a user searches for something or filters products on an e-commerce platform, that data is passed directly inside the URL string. Today, I learned how to intercept and decode that data natively!
🧠Key Learnings From Node.js Lecture 5 (The URL Module)
An incoming URL is much more than just a text path; it is a complex structured object. Here is the technical breakdown of how I dissected it today:
1. Ingesting the Native url Module
I explored Node's legacy and modern URL parsing engines. By passing the raw req.url string into the parser, Node breaks down the web address into a fully accessible metadata object.
2. Dissecting Pathname vs Query String
I learned the difference between the structural endpoint location and the dynamic data payload:
-
Pathname: The core location path (e.g.,
/searchor/api/products). -
Query: The actual data key-value strings attached after the question mark
?(e.g.,?name=ali&id=7).
javascript
const http = require("http");
const url = require("url");
const server = http.createServer((req, res) => {
// Parsing the URL path and query parameters together cleanly
let parsedUrl = url.parse(req.url, true);
let pathname = parsedUrl.pathname;
let queryData = parsedUrl.query; // Converts query text into a clean JS Object!
if (pathname === "/search") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(`Searching logs for user: ${queryData.name} with ID: ${queryData.id}`);
} else {
res.end("Standard Endpoint View");
}
});
server.listen(8000);
Top comments (0)