<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Creating Routes and Handling Requests with Express]]></title><description><![CDATA[Creating Routes and Handling Requests with Express]]></description><link>https://abhi-routes-handling.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1593680282896/kNC7E8IR4.png</url><title>Creating Routes and Handling Requests with Express</title><link>https://abhi-routes-handling.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 07:32:33 GMT</lastBuildDate><atom:link href="https://abhi-routes-handling.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Creating Routes and Handling Requests with Express]]></title><description><![CDATA[Introduction
Building web applications directly with Node.js HTTP modules can become repetitive and difficult as applications grow.
Express.js simplifies backend development by providing:

Cleaner rou]]></description><link>https://abhi-routes-handling.hashnode.dev/creating-routes-and-handling-requests-with-express</link><guid isPermaLink="true">https://abhi-routes-handling.hashnode.dev/creating-routes-and-handling-requests-with-express</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><category><![CDATA[routes]]></category><category><![CDATA[Requests]]></category><category><![CDATA[web app development]]></category><dc:creator><![CDATA[Abhishek Yadav]]></dc:creator><pubDate>Sat, 09 May 2026 21:01:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/697f608e9a52c3937f29cb4a/30293664-321c-4d0a-bc2f-7cdd989e684f.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Introduction</h2>
<p>Building web applications directly with Node.js HTTP modules can become repetitive and difficult as applications grow.</p>
<p>Express.js simplifies backend development by providing:</p>
<ul>
<li><p>Cleaner routing</p>
</li>
<li><p>Easier request handling</p>
</li>
<li><p>Simplified server setup</p>
</li>
<li><p>Better code organization</p>
</li>
</ul>
<p>This is why Express.js became one of the most popular Node.js frameworks.</p>
<p>In this article, you will learn how to create routes and handle requests using Express.</p>
<hr />
<h2>What is Express.js?</h2>
<p>Express.js is a lightweight web framework built on top of Node.js.</p>
<p>It helps developers build:</p>
<ul>
<li><p>APIs</p>
</li>
<li><p>Web applications</p>
</li>
<li><p>Backend servers</p>
</li>
</ul>
<p>without writing large amounts of low-level code.</p>
<hr />
<h2>Why Express Simplifies Node.js Development</h2>
<p>Without Express, creating routes and handling requests requires more manual work.</p>
<p>Express provides:</p>
<ul>
<li><p>Simple routing methods</p>
</li>
<li><p>Middleware support</p>
</li>
<li><p>Easy request and response handling</p>
</li>
<li><p>Cleaner project structure</p>
</li>
</ul>
<p>This makes development faster and more maintainable.</p>
<hr />
<h2>Raw Node.js HTTP Server Example</h2>
<h3>Using Node.js HTTP Module</h3>
<pre><code class="language-js">const http = require("http");

const server = http.createServer((req, res) =&gt; {
  if (req.url === "/") {
    res.write("Home Page");
    res.end();
  }
});

server.listen(3000);
</code></pre>
<p>Even simple routing requires manual conditions.</p>
<hr />
<h2>Express Server Example</h2>
<h3>Using Express.js</h3>
<pre><code class="language-js">const express = require("express");
const app = express();

app.get("/", (req, res) =&gt; {
  res.send("Home Page");
});

app.listen(3000);
</code></pre>
<p>The code becomes much cleaner and easier to read.</p>
<hr />
<h2>Installing Express</h2>
<p>Initialize project:</p>
<pre><code class="language-bash">npm init -y
</code></pre>
<p>Install Express:</p>
<pre><code class="language-bash">npm install express
</code></pre>
<hr />
<h2>Creating Your First Express Server</h2>
<pre><code class="language-js">const express = require("express");

const app = express();

app.listen(3000, () =&gt; {
  console.log("Server running on port 3000");
});
</code></pre>
<hr />
<h2>What is Routing?</h2>
<p>Routing means deciding how the server responds to different URLs and request types.</p>
<p>Example:</p>
<table>
<thead>
<tr>
<th>Route</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td><code>/</code></td>
<td>Home page</td>
</tr>
<tr>
<td><code>/users</code></td>
<td>User data</td>
</tr>
<tr>
<td><code>/products</code></td>
<td>Product data</td>
</tr>
</tbody></table>
<hr />
<h2>Express Routing Structure</h2>
<p>Client Request<br />↓<br />Express Route Matches URL<br />↓<br />Route Handler Executes<br />↓<br />Response Sent</p>
<hr />
<h2>Handling GET Requests</h2>
<p>GET requests are used to retrieve data.</p>
<h3>Example</h3>
<pre><code class="language-js">app.get("/users", (req, res) =&gt; {
  res.send("User List");
});
</code></pre>
<h3>Request</h3>
<pre><code class="language-txt">GET /users
</code></pre>
<h3>Response</h3>
<pre><code class="language-txt">User List
</code></pre>
<hr />
<h2>Handling POST Requests</h2>
<p>POST requests are used to send or create data.</p>
<h3>Example</h3>
<pre><code class="language-js">app.post("/users", (req, res) =&gt; {
  res.send("User Created");
});
</code></pre>
<h3>Request</h3>
<pre><code class="language-txt">POST /users
</code></pre>
<h3>Response</h3>
<pre><code class="language-txt">User Created
</code></pre>
<hr />
<h2>Understanding req and res</h2>
<p>Every route handler receives:</p>
<img src="https://cdn.hashnode.com/uploads/covers/697f608e9a52c3937f29cb4a/49b5bcb5-02cd-43b1-b03f-3b099693bcd0.png" alt="" style="display:block;margin:0 auto" />

<h3>req</h3>
<p>Represents the incoming request.</p>
<p>Contains:</p>
<ul>
<li><p>URL data</p>
</li>
<li><p>Headers</p>
</li>
<li><p>Query strings</p>
</li>
<li><p>Request body</p>
</li>
</ul>
<hr />
<h3>res</h3>
<p>Represents the server response.</p>
<p>Used to:</p>
<ul>
<li><p>Send text</p>
</li>
<li><p>Send JSON</p>
</li>
<li><p>Set status codes</p>
</li>
<li><p>End requests</p>
</li>
</ul>
<hr />
<h2>Sending Responses</h2>
<h3>Sending Text</h3>
<pre><code class="language-js">res.send("Hello World");
</code></pre>
<hr />
<h3>Sending JSON</h3>
<pre><code class="language-js">res.json({
  name: "Aman"
});
</code></pre>
<hr />
<h3>Sending Status Codes</h3>
<pre><code class="language-js">res.status(404).send("Not Found");
</code></pre>
<hr />
<h2>Example: Simple API</h2>
<pre><code class="language-js">const express = require("express");
const app = express();

app.get("/", (req, res) =&gt; {
  res.send("Welcome to API");
});

app.get("/users", (req, res) =&gt; {
  res.json([
    { id: 1, name: "Aman" },
    { id: 2, name: "Riya" }
  ]);
});

app.post("/users", (req, res) =&gt; {
  res.send("New user created");
});

app.listen(3000, () =&gt; {
  console.log("Server started");
});
</code></pre>
<hr />
<h2>Request → Route → Response Flow</h2>
<p><img src="align=%22center%22" alt="" /></p>
<p>Browser / Client<br />↓<br />Request Sent<br />↓<br />Express Matches Route<br />↓<br />Route Handler Executes<br />↓<br />Response Returned</p>
<hr />
<h2>Why Express Became Popular</h2>
<img src="https://cdn.hashnode.com/uploads/covers/697f608e9a52c3937f29cb4a/7db74c89-7373-4c7e-a1ed-094b4b1595fd.png" alt="" style="display:block;margin:0 auto" />

<p>Developers adopted Express because it:</p>
<ul>
<li><p>Reduces boilerplate code</p>
</li>
<li><p>Simplifies routing</p>
</li>
<li><p>Makes APIs easier to build</p>
</li>
<li><p>Supports middleware architecture</p>
</li>
<li><p>Integrates well with Node.js ecosystem</p>
</li>
</ul>
<hr />
<h2>Common Beginner Mistakes</h2>
<h3>Forgetting app.listen()</h3>
<p>Without:</p>
<pre><code class="language-js">app.listen()
</code></pre>
<p>server will not start.</p>
<hr />
<h3>Using Wrong HTTP Method</h3>
<p>GET and POST routes are different.</p>
<pre><code class="language-js">app.get()
app.post()
</code></pre>
<p>must match request type.</p>
<hr />
<h3>Sending Multiple Responses</h3>
<p>Each request should receive only one response.</p>
<img src="https://cdn.hashnode.com/uploads/covers/697f608e9a52c3937f29cb4a/a119de0e-066e-4cf7-a70d-16b81801c98d.png" alt="" style="display:block;margin:0 auto" />

<hr />
<h2>Key Takeaways</h2>
<ul>
<li><p>Express.js simplifies Node.js backend development</p>
</li>
<li><p>Routing controls server behavior for URLs</p>
</li>
<li><p>GET requests fetch data</p>
</li>
<li><p>POST requests send data</p>
</li>
<li><p>req stores request information</p>
</li>
<li><p>res sends responses back to client</p>
</li>
</ul>
<hr />
<h2>Conclusion</h2>
<p>Express.js makes backend development in Node.js significantly simpler by reducing repetitive code and providing powerful routing features.</p>
<p>Understanding routes and request handling is the foundation for building APIs and scalable backend systems.</p>
<hr />
<h2>Final Tip</h2>
<p>Before learning advanced Express concepts like middleware or authentication, first become comfortable with:</p>
<ul>
<li><p>Routes</p>
</li>
<li><p>Request handling</p>
</li>
<li><p>Responses</p>
</li>
<li><p>HTTP methods</p>
</li>
</ul>
<p>These fundamentals form the backbone of every Express application.</p>
]]></content:encoded></item></channel></rss>