<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged bun</title>
<link>https://coderlegion.com/tag/bun</link>
<description>Powered by Question2Answer</description>
<item>
<title>Building Web Servers with Node.js and Bun – A Backend Developer’s First Step</title>
<link>https://coderlegion.com/4293/building-web-servers-with-node-js-and-bun-a-backend-developers-first-step</link>
<description>&lt;h2 id=&quot;welcome-back-to-the-backend-development-series-&quot;&gt;Welcome Back to the Backend Development Series!&lt;/h2&gt;
&lt;p&gt;In the &lt;strong&gt;previous post&lt;/strong&gt;, we laid the groundwork:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;✅ We explained what a &lt;strong&gt;web server&lt;/strong&gt; is&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ We explored the &lt;strong&gt;Client-Server Architecture&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ We saw how &lt;strong&gt;JavaScript can run on the backend&lt;/strong&gt; using &lt;strong&gt;Node.js&lt;/strong&gt; and &lt;strong&gt;Bun&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ And most importantly, we &lt;strong&gt;built our very first web server&lt;/strong&gt; using Node’s built-in &lt;code&gt;http&lt;/code&gt; module&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But that server had one big limitation...&lt;/p&gt;
&lt;h3 id=&quot;-the-problem-with-our-first-server&quot;&gt;⚠️ The Problem With Our First Server&lt;/h3&gt;
&lt;p&gt;In our first server, no matter what URL you visited — whether it was &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;/about&lt;/code&gt;, &lt;code&gt;/ice-tea&lt;/code&gt;, or even &lt;code&gt;/random-nonsense&lt;/code&gt; — the server would respond with the &lt;strong&gt;exact same message&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;Hello from your first Node server!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why? Because we didn’t check what URL the user was actually requesting.&lt;br&gt;
We just responded blindly to &lt;strong&gt;every single request&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And that’s not how real-world websites work, right?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/login&lt;/code&gt; should show a login form&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/about&lt;/code&gt; should show info about your site&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/ice-tea&lt;/code&gt; should confirm your tea order ☕&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This brings us to an important feature in every backend system:&lt;br&gt;
 &lt;strong&gt;Routing&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;what-is-routing-&quot;&gt;What Is Routing?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Routing&lt;/strong&gt; is the logic that tells your server:&lt;br&gt;
➡️ &lt;em&gt;“If a user visits this path (&lt;/em&gt;&lt;code&gt;/&lt;/code&gt;), respond with this content.&lt;br&gt;
If they visit that path (&lt;code&gt;/ice-tea&lt;/code&gt;), respond differently.”&lt;/p&gt;
&lt;p&gt;In simple terms:&lt;br&gt;
&lt;strong&gt;Different URLs → Different responses&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;what-you-ll-learn-in-this-blog&quot;&gt;What You’ll Learn in This Blog&lt;/h3&gt;
&lt;p&gt;We’re going hands-on again — but this time, we’ll build &lt;strong&gt;two smart servers&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;Vanilla Node.js&lt;/strong&gt; server (with manual routing using &lt;code&gt;if-else&lt;/code&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;Bun&lt;/strong&gt; server (with its built-in speedy tools)&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By the end, you'll understand:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;✅ How to respond to specific paths like &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;/login&lt;/code&gt;, &lt;code&gt;/ice-tea&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ How routing works &lt;em&gt;without&lt;/em&gt; any frameworks&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ Why &lt;strong&gt;developer experience matters&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ And why frameworks like &lt;strong&gt;Express.js&lt;/strong&gt; were born to save our sanity&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s start with Node.js.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-building-a-smarter-web-server-in-node-js-vanilla-style-&quot;&gt;️ Building a Smarter Web Server in Node.js (Vanilla Style)&lt;/h2&gt;
&lt;h4 id=&quot;step-1-create-a-file&quot;&gt;Step 1: Create a File&lt;/h4&gt;
&lt;p&gt;Create a new file named:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;server-node.js
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h4 id=&quot;step-2-add-this-code&quot;&gt;Step 2: Add This Code&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;const http = require(&quot;http&quot;);

const hostname = &quot;127.0.0.1&quot;;
const port = 3000;

const server = http.createServer((req, res) =&amp;gt; {
  if (req.url === &quot;/&quot;) {
    res.statusCode = 200;
    res.setHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;);
    res.end(&quot;Hello, it's Ice Tea!&quot;);
  } else if (req.url === &quot;/ice-tea&quot;) {
    res.statusCode = 200;
    res.setHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;);
    res.end(&quot;Thanks for ordering ice tea. It's really hot!&quot;);
  } else {
    res.statusCode = 404;
    res.setHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;);
    res.end(&quot;404 - Not Found&quot;);
  }
});

server.listen(port, hostname, () =&amp;gt; {
  console.log(`Server is listening at http://${hostname}:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;what-s-happening-here-&quot;&gt;What’s Happening Here?&lt;/h4&gt;
&lt;p&gt;Let’s break it down:&lt;/p&gt;
&lt;h5 id=&quot;req-url&quot;&gt;&lt;code&gt;req.url&lt;/code&gt;&lt;/h5&gt;
&lt;p&gt;This gives us the &lt;strong&gt;path&lt;/strong&gt; the user is trying to access. Examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/ice-tea&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;/something-weird&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We use this value to decide &lt;strong&gt;what response&lt;/strong&gt; to send back.&lt;/p&gt;
&lt;h5 id=&quot;if-else-if-else&quot;&gt;&lt;code&gt;if...else if...else&lt;/code&gt;&lt;/h5&gt;
&lt;p&gt;This is basic routing logic:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If the user visits &lt;code&gt;/&lt;/code&gt; → show the homepage&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If they visit &lt;code&gt;/ice-tea&lt;/code&gt; → give them a virtual cup ☕&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If they visit anything else → show a &lt;code&gt;404 - Not Found&lt;/code&gt; message&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-how-to-run-it&quot;&gt;▶️ How to Run It&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open your terminal&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the server:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;node server-node.js
&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Open your browser and go to:&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/&lt;/code&gt;&lt;/a&gt; → You’ll see: &lt;code&gt;Hello, it's Ice Tea!&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/ice-tea&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/ice-tea&lt;/code&gt;&lt;/a&gt; → You’ll see: &lt;code&gt;Thanks for ordering ice tea. It's really hot!&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/anything-else&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/anything-else&lt;/code&gt;&lt;/a&gt; → You’ll see: &lt;code&gt;404 - Not Found&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;why-this-matters&quot;&gt;Why This Matters&lt;/h4&gt;
&lt;p&gt;In our first server (from the last post), &lt;strong&gt;we didn’t handle routes at all&lt;/strong&gt;.&lt;br&gt;
The result? Every request got the same reply. That’s not practical.&lt;/p&gt;
&lt;p&gt;This version adds &lt;strong&gt;conditional logic&lt;/strong&gt; — a baby step toward how real websites handle pages, endpoints, and APIs.&lt;/p&gt;
&lt;p&gt;You’ve just built your first &lt;strong&gt;routing mechanism&lt;/strong&gt; in Node.js!&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;rebuilding-the-same-server-with-bun&quot;&gt;Rebuilding the Same Server with Bun&lt;/h2&gt;
&lt;p&gt;We’ve already built a basic web server in Node.js using &lt;code&gt;http.createServer&lt;/code&gt; and some conditional routing.&lt;/p&gt;
&lt;p&gt;Now let’s &lt;strong&gt;recreate the exact same logic&lt;/strong&gt;, but this time using &lt;strong&gt;Bun&lt;/strong&gt; — the newer, faster JavaScript runtime that’s shaking up the backend ecosystem.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;✅ If Node.js feels like a power drill, Bun is more like a Swiss Army knife — fast, modern, and loaded with built-in tools.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;h4 id=&quot;step-1-create-a-file-1&quot;&gt;Step 1: Create a File&lt;/h4&gt;
&lt;p&gt;Create a new file named:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;server-bun.js
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h4 id=&quot;step-2-add-this-code-1&quot;&gt;Step 2: Add This Code&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;import { serve } from &quot;bun&quot;;

serve({
  fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === &quot;/&quot;) {
      return new Response(&quot;Hello, it's Ice Tea!&quot;, { status: 200 });
    } else if (url.pathname === &quot;/ice-tea&quot;) {
      return new Response(&quot;Ice tea is a good option!&quot;, { status: 200 });
    } else {
      return new Response(&quot;404 - Not Found&quot;, { status: 404 });
    }
  },
  port: 3000,
  hostname: &quot;127.0.0.1&quot;
});
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h4 id=&quot;what-s-going-on-here-&quot;&gt;What’s Going On Here?&lt;/h4&gt;
&lt;p&gt;Let’s break it down piece by piece:&lt;/p&gt;
&lt;h5 id=&quot;-import-serve-from-bun-&quot;&gt;✅ &lt;code&gt;import { serve } from &quot;bun&quot;&lt;/code&gt;&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No need for &lt;code&gt;http&lt;/code&gt; module or third-party packages — Bun comes with &lt;code&gt;serve&lt;/code&gt; &lt;strong&gt;built-in&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This function spins up a server instantly with minimal setup&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;-fetch-req-&quot;&gt;✅ &lt;code&gt;fetch(req)&lt;/code&gt;&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Instead of &lt;code&gt;createServer((req, res) =&amp;gt; {})&lt;/code&gt;, Bun uses a &lt;code&gt;fetch()&lt;/code&gt;-like handler&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you've ever used &lt;code&gt;fetch()&lt;/code&gt; in the browser, this will feel familiar&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This function receives the request and is expected to return a &lt;code&gt;Response&lt;/code&gt; object&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;-new-url-req-url-&quot;&gt;✅ &lt;code&gt;new URL(req.url)&lt;/code&gt;&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This gives you an easy way to extract the &lt;code&gt;pathname&lt;/code&gt; (like &lt;code&gt;/&lt;/code&gt; or &lt;code&gt;/ice-tea&lt;/code&gt;) from the request URL&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Think of it like a more modern and cleaner way than parsing &lt;code&gt;req.url&lt;/code&gt; manually in Node&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;-return-new-response-&quot;&gt;✅ &lt;code&gt;return new Response(...)&lt;/code&gt;&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;Just like the browser's &lt;code&gt;fetch()&lt;/code&gt; API — you return a &lt;code&gt;Response&lt;/code&gt; object with a body and status code&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;what-you-just-did&quot;&gt;What You Just Did&lt;/h4&gt;
&lt;p&gt;You created a complete HTTP server using only &lt;strong&gt;10 lines of code&lt;/strong&gt;, with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No external dependencies&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No manual headers&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No boilerplate setup&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And it &lt;strong&gt;still behaves exactly like the Node.js version&lt;/strong&gt;:&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-how-to-run-it-1&quot;&gt;▶️ How to Run It&lt;/h4&gt;
&lt;p&gt;Make sure you have Bun installed. Then run:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;bun server-bun.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, visit these URLs in your browser:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;URL &lt;/th&gt;&lt;th&gt;Response&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/&lt;/code&gt;&lt;/a&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;Hello, it's Ice Tea!&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/ice-tea&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/ice-tea&lt;/code&gt;&lt;/a&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;Ice tea is a good option!&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/random&quot;&gt;&lt;code&gt;http://127.0.0.1:3000/random&lt;/code&gt;&lt;/a&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;404 - Not Found&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;key-learnings-node-js-vs-bun-syntax-comparison&quot;&gt;Key Learnings – Node.js vs Bun Syntax Comparison&lt;/h2&gt;
&lt;p&gt;Let’s break down how Node.js and Bun differ in terms of syntax and developer experience:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;&lt;strong&gt;Feature&lt;/strong&gt; &lt;/th&gt;&lt;th&gt;&lt;strong&gt;Node.js&lt;/strong&gt; &lt;/th&gt;&lt;th&gt;&lt;strong&gt;Bun&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Imports&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;require(&quot;http&quot;)&lt;/code&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;import { serve } from &quot;bun&quot;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Server creation&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;http.createServer()&lt;/code&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;serve()&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Request object&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;req.url&lt;/code&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;new URL(req.url)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Response&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;res.writeHead(), res.end()&lt;/code&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;return new Response(...)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Port/Host setup&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&lt;code&gt;server.listen(port, host)&lt;/code&gt; &lt;/td&gt;&lt;td&gt;Included directly in &lt;code&gt;serve({})&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;File support&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;CommonJS (&lt;code&gt;.js&lt;/code&gt;) &lt;/td&gt;&lt;td&gt;ESM-first (&lt;code&gt;.js&lt;/code&gt;, &lt;code&gt;.ts&lt;/code&gt;, &lt;code&gt;.jsx&lt;/code&gt;, etc.)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Auto Restart&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;❌ Manual restart required &lt;/td&gt;&lt;td&gt;❌ Not built-in (use &lt;code&gt;bunx nodemon&lt;/code&gt; or &lt;code&gt;bun --watch&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-the-problem-code-reusability-amp-scalability&quot;&gt;⚠️ The Problem: Code Reusability &amp;amp; Scalability&lt;/h2&gt;
&lt;p&gt;Let’s face it — even with just &lt;strong&gt;3 routes&lt;/strong&gt;, our &lt;code&gt;if-else&lt;/code&gt; chain already looks messy.&lt;/p&gt;
&lt;p&gt;Imagine managing 20+ routes in a real-world API this way. &lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Problem &lt;/th&gt;&lt;th&gt;Why It Matters&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;❌ Hard to read &lt;/td&gt;&lt;td&gt;Too much nesting, unclear logic&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;❌ Not modular &lt;/td&gt;&lt;td&gt;All logic in one file — no separation&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;❌ Difficult to test &lt;/td&gt;&lt;td&gt;No reusable structure, can't isolate parts&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;the-solution-use-a-framework&quot;&gt;The Solution: Use a Framework&lt;/h2&gt;
&lt;p&gt;That’s why developers turn to &lt;strong&gt;backend frameworks&lt;/strong&gt; — to make their code cleaner, modular, and scalable.&lt;/p&gt;
&lt;p&gt;Some popular options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Express&lt;/strong&gt; (Node.js) – industry standard&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hono&lt;/strong&gt; (Bun) – minimal, super fast&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fastify&lt;/strong&gt; – fast, schema-based&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Koa&lt;/strong&gt; – modern, middleware-centric&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These frameworks provide:&lt;/p&gt;
&lt;p&gt;✅ Route grouping&lt;br&gt;
✅ Middleware support (auth, logging, etc.)&lt;br&gt;
✅ Centralized error handling&lt;br&gt;
✅ Clean, readable syntax&lt;br&gt;
✅ Better DX (Developer Experience)&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;what-s-next-&quot;&gt;What’s &lt;code&gt;Next&lt;/code&gt;?&lt;/h2&gt;
&lt;p&gt;In the next blog, we’ll take things to the next level:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Explore backend frameworks&lt;/strong&gt; (not just Express)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start using Express.js&lt;/strong&gt; to build cleaner, modular, and testable APIs&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learn about middleware, route grouping, and error handling&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;You’ve just taken your first real step toward backend mastery.&lt;/strong&gt;&lt;br&gt;
If you found this post helpful, just wait till we dive into frameworks — things are about to get &lt;em&gt;way more powerful&lt;/em&gt; (and easier)!&lt;/p&gt;
&lt;p&gt;Stay tuned.&lt;/p&gt;
</description>
<category>Setup Guide</category>
<guid isPermaLink="true">https://coderlegion.com/4293/building-web-servers-with-node-js-and-bun-a-backend-developers-first-step</guid>
<pubDate>Mon, 28 Jul 2025 06:12:04 +0000</pubDate>
</item>
<item>
<title>Getting Started with Backend Development: Web Servers, Node.js, and Bun Explained</title>
<link>https://coderlegion.com/4186/getting-started-with-backend-development-web-servers-node-js-and-bun-explained</link>
<description>&lt;p&gt;Hey everyone!&lt;br&gt;
If you’re ready to dive into the world of backend development, you’re in the right place. This blog kicks off our backend dev series—where we’re going beyond the frontend and digging into the engines powering the web: servers, requests, runtime environments, and real-world backend architecture.&lt;/p&gt;
&lt;p&gt;Whether you're aiming to build large-scale applications like Shopify or simple ToDo apps, &lt;strong&gt;understanding how a backend works is absolutely essential&lt;/strong&gt;. And yes, we’ll write actual code along the way — not just talk theory.&lt;/p&gt;
&lt;p&gt;Let’s get started!&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-s-the-prerequisite-&quot;&gt;What's the Prerequisite?&lt;/h3&gt;
&lt;p&gt;You only need one thing before jumping in:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Basic knowledge of JavaScript&lt;/strong&gt; — just the fundamentals like variables, functions, and loops.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No need to master Node.js, Bun, or understand how backend protocols work. You don’t even need to know what a server really is.&lt;/p&gt;
&lt;p&gt;This blog is designed for absolute beginners in backend development. We’ll start from scratch and build everything &lt;strong&gt;step by step&lt;/strong&gt; — explaining not just the &lt;em&gt;how&lt;/em&gt;, but also the &lt;em&gt;why&lt;/em&gt; behind each concept.&lt;/p&gt;
&lt;p&gt;By the end, backend buzzwords like &lt;em&gt;HTTP&lt;/em&gt;, &lt;em&gt;status codes&lt;/em&gt;, &lt;em&gt;request/response cycle&lt;/em&gt;, and &lt;em&gt;routing&lt;/em&gt; will feel like second nature.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;understanding-backend-architecture&quot;&gt;Understanding Backend Architecture&lt;/h3&gt;
&lt;p&gt;Before we jump into the code, let’s take a moment to understand &lt;strong&gt;what we’re actually building&lt;/strong&gt; and how it fits into the bigger picture of web development.&lt;/p&gt;
&lt;h4 id=&quot;client-server-architecture&quot;&gt;Client-Server Architecture&lt;/h4&gt;
&lt;p&gt;At the heart of every backend system lies a simple but powerful concept: &lt;strong&gt;the client-server model&lt;/strong&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;Client (Browser / Mobile App) → Web Server → Database (Optional)
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client&lt;/strong&gt;: This is the user-facing side — it could be a browser, mobile app, smartwatch, or anything capable of making a request.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web Server&lt;/strong&gt;: The brain of your backend. It receives client requests, processes them, and sends back a response. This is our main focus in this series.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Database&lt;/strong&gt; &lt;em&gt;(not covered yet)&lt;/em&gt;: A place to store, retrieve, and manage data. Examples include MongoDB, PostgreSQL, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this blog, we’ll &lt;strong&gt;focus solely on building the web server&lt;/strong&gt; — step by step — so you understand exactly what’s happening under the hood.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-is-a-web-server-&quot;&gt;What is a Web Server?&lt;/h3&gt;
&lt;p&gt;Let’s bust a myth:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;❌ A server isn’t always a giant machine in a datacenter.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;✅ A web server is simply a &lt;strong&gt;piece of software&lt;/strong&gt; whose job is to &lt;strong&gt;receive requests and send responses&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No matter the language — PHP, .NET, Node.js, Python — the core idea stays the same:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;Listen on a port → Process the request → Send a response
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can even build a web server on your &lt;strong&gt;local laptop&lt;/strong&gt; — and that’s exactly what we’ll do.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;node-js-vs-bun-javascript-runtimes-for-the-backend&quot;&gt;Node.js vs Bun – JavaScript Runtimes for the Backend&lt;/h3&gt;
&lt;h4 id=&quot;first-what-s-a-runtime-&quot;&gt;First, What’s a Runtime?&lt;/h4&gt;
&lt;p&gt;Before comparing Node.js and Bun, let’s answer a basic question:&lt;br&gt;
&lt;strong&gt;JavaScript was originally made to run in browsers... so how are we using it to build backend servers?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The answer: &lt;strong&gt;JavaScript Runtime Environments&lt;/strong&gt; — tools that allow JavaScript to run &lt;strong&gt;outside the browser&lt;/strong&gt; (on your computer or a server).&lt;/p&gt;
&lt;p&gt;These runtimes provide:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access to the &lt;strong&gt;file system&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ability to &lt;strong&gt;create servers&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tools for &lt;strong&gt;running scripts&lt;/strong&gt;, installing packages, and much more&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s explore the two most popular runtimes today: &lt;strong&gt;Node.js&lt;/strong&gt; and &lt;strong&gt;Bun&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&quot;node-js-the-veteran-in-the-game&quot;&gt;Node.js – The Veteran in the Game&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;eleased:&lt;/strong&gt; 2009&lt;br&gt;
&lt;strong&gt;Used by:&lt;/strong&gt; Netflix, PayPal, LinkedIn, Walmart, and many more&lt;br&gt;
&lt;strong&gt;Built with:&lt;/strong&gt; C++, JavaScript, and Google Chrome’s V8 engine&lt;/p&gt;
&lt;h5 id=&quot;key-features-&quot;&gt;Key Features:&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event-Driven, Non-blocking I/O:&lt;/strong&gt;&lt;br&gt;
  Perfect for handling thousands of concurrent network requests — great for APIs, real-time apps (like chats), and more.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Massive Package Ecosystem (npm):&lt;/strong&gt;&lt;br&gt;
  With over &lt;strong&gt;2 million packages&lt;/strong&gt;, npm is the largest open-source library in the world. You can find ready-made solutions for almost anything.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cross-Platform:&lt;/strong&gt;&lt;br&gt;
  Runs on Windows, macOS, and Linux with minimal setup.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Active Community &amp;amp; Support:&lt;/strong&gt;&lt;br&gt;
  Tons of tutorials, Stack Overflow answers, and official documentation to help you.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Used in Production Everywhere:&lt;/strong&gt;&lt;br&gt;
  Node.js powers high-scale applications like Discord, Trello, and Uber.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;-typical-use-cases-&quot;&gt;️ Typical Use Cases:&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;RESTful APIs&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time apps (WebSocket servers)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Microservices&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Backend for web/mobile apps&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CLI tools&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h4 id=&quot;bun-the-new-speedster&quot;&gt;Bun – The New Speedster&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Released:&lt;/strong&gt; 2022 (very recent)&lt;br&gt;
&lt;strong&gt;Built with:&lt;/strong&gt; Zig programming language (known for speed and safety)&lt;br&gt;
&lt;strong&gt;Engine:&lt;/strong&gt; JavaScriptCore (used by Safari)&lt;/p&gt;
&lt;h5 id=&quot;key-features--1&quot;&gt;Key Features:&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Blazing Fast Performance:&lt;/strong&gt;&lt;br&gt;
  Up to &lt;strong&gt;3x faster than Node.js&lt;/strong&gt; in some benchmarks. Ideal for high-performance apps.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;All-in-One Toolchain:&lt;/strong&gt;&lt;br&gt;
  Bun replaces several tools in the Node ecosystem with one unified solution:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Feature &lt;/th&gt;&lt;th&gt;Bun Replaces&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;HTTP Server &lt;/td&gt;&lt;td&gt;&lt;code&gt;express&lt;/code&gt;, &lt;code&gt;http&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Package Manager &lt;/td&gt;&lt;td&gt;&lt;code&gt;npm&lt;/code&gt;, &lt;code&gt;yarn&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Task Runner &lt;/td&gt;&lt;td&gt;&lt;code&gt;npm scripts&lt;/code&gt;, &lt;code&gt;gulp&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Bundler &lt;/td&gt;&lt;td&gt;&lt;code&gt;webpack&lt;/code&gt;, &lt;code&gt;esbuild&lt;/code&gt;, &lt;code&gt;vite&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Test Runner &lt;/td&gt;&lt;td&gt;&lt;code&gt;jest&lt;/code&gt;, &lt;code&gt;mocha&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-in TypeScript Support:&lt;/strong&gt;&lt;br&gt;
  No need to set up TypeScript compilers or configs — it just works.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimal Setup:&lt;/strong&gt;&lt;br&gt;
  Fewer configuration files, faster install times, and cleaner development workflow.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;things-to-keep-in-mind-&quot;&gt;Things to Keep in Mind:&lt;/h5&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Still &lt;strong&gt;new and evolving&lt;/strong&gt; — some npm packages might not work perfectly yet&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Smaller community compared to Node.js&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Best suited for newer projects or experimental builds&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;our-approach-in-this-blog&quot;&gt;Our Approach in This Blog&lt;/h4&gt;
&lt;p&gt;You &lt;strong&gt;don’t need to master Node.js or Bun&lt;/strong&gt; before starting.&lt;/p&gt;
&lt;p&gt;We'll focus on &lt;strong&gt;Node.js&lt;/strong&gt; throughout this series for three reasons:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It’s more stable and widely adopted&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Most job roles still prefer Node.js experience&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tons of beginner resources are available for support&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But you’re always welcome to explore Bun once you’re comfortable!&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;installing-node-js-and-bun&quot;&gt;Installing Node.js and Bun&lt;/h3&gt;
&lt;p&gt;Both are available for all major OS:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Node.js:&lt;/strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://nodejs.org&quot;&gt;https://nodejs.org&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bun:&lt;/strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://bun.sh&quot;&gt;https://bun.sh&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;&lt;p&gt;For this lesson, we’ll start by using &lt;strong&gt;Node.js&lt;/strong&gt; to create a basic HTTP server.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;h3 id=&quot;let-s-build-your-first-web-server-in-node-js&quot;&gt;Let's Build: Your First Web Server in Node.js&lt;/h3&gt;
&lt;p&gt;Here’s how you get started:&lt;/p&gt;
&lt;h4 id=&quot;step-by-step-setup&quot;&gt;Step-by-Step Setup&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a project folder&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;mkdir backend &amp;amp;&amp;amp; cd backend
mkdir 01-web-server &amp;amp;&amp;amp; cd 01-web-server
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open it in VS Code&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a file&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;server-node.js
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Check Node Installation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;node -v
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;code-creating-an-http-server-in-node-js&quot;&gt;Code: Creating an HTTP Server in Node.js&lt;/h4&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// 1. Import Node's built-in http module
const http = require(&quot;http&quot;);

// 2. Define hostname and port
const hostname = &quot;127.0.0.1&quot;;
const port = 3000;

// 3. Create a server instance with a callback
const server = http.createServer((req, res) =&amp;gt; {
  res.statusCode = 200; // OK
  res.setHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;);
  res.end(&quot;Hello from your first Node server!&quot;);
});

// 4. Start listening
server.listen(port, hostname, () =&amp;gt; {
  console.log(`Server running at http://${hostname}:${port}/`);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt; This creates a local server that responds to every request with &lt;code&gt;&quot;Hello from your first Node server!&quot;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;h3 id=&quot;how-this-works-a-breakdown&quot;&gt;How This Works – A Breakdown&lt;/h3&gt;
&lt;p&gt;Let’s write our first backend program — a simple HTTP server using Node’s built-in &lt;code&gt;http&lt;/code&gt; module.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// 1. Import Node's built-in http module
const http = require(&quot;http&quot;);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;http&lt;/code&gt; is a &lt;strong&gt;core module&lt;/strong&gt; in Node.js — no need to install it.&lt;br&gt;
It provides functions to create a web server and handle HTTP requests/responses.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// 2. Define hostname and port
const hostname = &quot;127.0.0.1&quot;;
const port = 3000;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;hostname&lt;/code&gt;: &lt;code&gt;127.0.0.1&lt;/code&gt; refers to &lt;a rel=&quot;nofollow&quot; href=&quot;http://localhost&quot;&gt;&lt;strong&gt;localhost&lt;/strong&gt;&lt;/a&gt; — your own computer.&lt;br&gt;
 &lt;code&gt;port&lt;/code&gt;: A unique number used by the server to listen for requests (3000 is a common dev port).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// 3. Create a server instance with a callback
const server = http.createServer((req, res) =&amp;gt; {
  res.statusCode = 200; // OK
  res.setHeader(&quot;Content-Type&quot;, &quot;text/plain&quot;);
  res.end(&quot;Hello from your first Node server!&quot;);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;http.createServer()&lt;/code&gt; creates the server.&lt;br&gt;
 It takes a callback with two parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;req&lt;/code&gt; = the &lt;strong&gt;incoming request&lt;/strong&gt; from the client&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;res&lt;/code&gt; = the &lt;strong&gt;response&lt;/strong&gt; you send back&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Inside the callback:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;res.statusCode = 200&lt;/code&gt; → means “OK” (request was successful)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;res.setHeader()&lt;/code&gt; → tells the client that the content is plain text&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;res.end()&lt;/code&gt; → finalizes the response and sends it to the client&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;// 4. Start listening
server.listen(port, hostname, () =&amp;gt; {
  console.log(`Server running at http://${hostname}:${port}/`);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;code&gt;.listen()&lt;/code&gt; starts the server on the specified &lt;code&gt;hostname&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt;.&lt;br&gt;
 The callback logs a message to the terminal once the server is up and running.&lt;/p&gt;
&lt;h4 id=&quot;output-&quot;&gt;Output:&lt;/h4&gt;
&lt;p&gt;When you run the file with:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;node server-node.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Open your browser or send a request from postman and go to:&lt;br&gt;
 &lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000￼You’ll&quot;&gt;&lt;code&gt;http://127.0.0.1:3000&lt;/code&gt;&lt;br&gt;
You’ll&lt;/a&gt; see:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;Hello from your first Node server!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Boom!  You've just created your first backend server.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;why-this-matters-1&quot;&gt;Why This Matters&lt;/h3&gt;
&lt;p&gt;Understanding this flow sets the foundation for everything you’ll build later:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Express.js&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;REST APIs&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;File serving&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Auth systems&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database communication&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you know how to create and manage a server at this raw level, the frameworks and tools later on will make a lot more sense.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;summary-what-we-covered&quot;&gt;Summary – What We Covered&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Topic &lt;/th&gt;&lt;th&gt;Key Takeaways&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Backend Architecture &lt;/td&gt;&lt;td&gt;Client → Server → Database (we focused on server)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Web Server &lt;/td&gt;&lt;td&gt;Just software that serves responses&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;JavaScript Runtime &lt;/td&gt;&lt;td&gt;Node.js and Bun allow JS to run outside the browser&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Code &lt;/td&gt;&lt;td&gt;You wrote your first HTTP server in Node.js&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Philosophy &lt;/td&gt;&lt;td&gt;Learn by building, no need to “master” Node first&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-s-next--1&quot;&gt;What's Next?&lt;/h3&gt;
&lt;p&gt;In the upcoming parts of this blog series&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;we will see handling different routes and HTTP methods in this Node server.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We’ll explore how to write web servers using &lt;strong&gt;Bun&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try to understand the need of these frameworks&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Until then, play around with the Node.js server.&lt;br&gt;
Try changing the port, printing the request URL, or sending different responses. Try sending a request from &lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/login&quot;&gt;http://127.0.0.1:3000/login&lt;/a&gt; or &lt;a rel=&quot;nofollow&quot; href=&quot;http://127.0.0.1:3000/hello&quot;&gt;http://127.0.0.1:3000/hello&lt;/a&gt; or any other url.&lt;/p&gt;
&lt;p&gt;Try to find out what’s wrong with our present server XD !!!!!!!&lt;/p&gt;
</description>
<category>Getting Started</category>
<guid isPermaLink="true">https://coderlegion.com/4186/getting-started-with-backend-development-web-servers-node-js-and-bun-explained</guid>
<pubDate>Sun, 20 Jul 2025 19:00:38 +0000</pubDate>
</item>
</channel>
</rss>