Complete the code to create your first Node.js application:
📦 Node.js-> Command Line
✨ The Prompt Phrase
let http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
💻 Code Preview
📦 All-in-One Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Your First Node.js HTTP Server - Interactive Tutorial</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&family=Fira+Code:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg-primary: #0a0e27;
--bg-secondary: #151932;
--bg-card: #1e2139;
--accent-purple: #a855f7;
--accent-blue: #3b82f6;
--accent-green: #10b981;
--accent-red: #ef4444;
--accent-yellow: #f59e0b;
--accent-orange: #f97316;
--text-primary: #f8fafc;
--text-secondary: #94a3b8;
--gradient-1: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--gradient-2: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
--gradient-3: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
--gradient-server: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
--shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
line-height: 1.6;
overflow-x: hidden;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Hero Section */
.hero {
text-align: center;
padding: 100px 20px;
background: var(--gradient-server);
position: relative;
overflow: hidden;
}
.hero::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('data:image/svg+xml,<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="2" fill="white" opacity="0.1"/></svg>');
animation: float 20s linear infinite;
}
@keyframes float {
from { transform: translateY(0); }
to { transform: translateY(-100px); }
}
.hero-content {
position: relative;
z-index: 1;
}
.command-display {
display: inline-block;
background: rgba(0, 0, 0, 0.3);
padding: 20px 30px;
border-radius: 15px;
font-family: 'Fira Code', monospace;
font-size: 1.2rem;
margin: 30px 0;
border: 2px solid rgba(255, 255, 255, 0.2);
animation: pulse 2s ease-in-out infinite;
max-width: 90%;
text-align: left;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.02); }
}
.hero h1 {
font-size: 3rem;
font-weight: 700;
margin-bottom: 20px;
animation: slideDown 0.8s ease-out;
}
.hero p {
font-size: 1.3rem;
opacity: 0.95;
animation: slideUp 0.8s ease-out;
}
@keyframes slideDown {
from { transform: translateY(-50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
@keyframes slideUp {
from { transform: translateY(50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
/* Progress Bar */
.progress-container {
position: sticky;
top: 0;
width: 100%;
height: 5px;
background: var(--bg-secondary);
z-index: 1000;
}
.progress-bar {
height: 100%;
background: var(--gradient-3);
width: 0%;
transition: width 0.3s ease;
}
/* Section Styles */
.section {
margin: 60px 0;
animation: fadeIn 0.8s ease-out;
animation-fill-mode: both;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(30px); }
to { opacity: 1; transform: translateY(0); }
}
.section-title {
font-size: 2.5rem;
margin-bottom: 30px;
background: var(--gradient-3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
display: inline-block;
}
.card {
background: var(--bg-card);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
box-shadow: var(--shadow);
border: 1px solid rgba(255, 255, 255, 0.05);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 25px 70px rgba(0, 0, 0, 0.4);
}
/* Code Block */
.code-block {
background: #1e1e1e;
border-radius: 15px;
padding: 25px;
margin: 20px 0;
position: relative;
overflow: hidden;
font-family: 'Fira Code', monospace;
}
.code-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.code-lang {
color: var(--accent-green);
font-weight: 600;
}
.copy-btn {
background: var(--accent-purple);
color: white;
border: none;
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.3s ease;
}
.copy-btn:hover {
background: var(--accent-blue);
transform: scale(1.05);
}
.copy-btn.copied {
background: var(--accent-green);
}
pre {
margin: 0;
overflow-x: auto;
}
code {
color: #e0e0e0;
font-size: 0.95rem;
line-height: 1.8;
}
.code-comment {
color: #6a9955;
}
.code-keyword {
color: #569cd6;
}
.code-string {
color: #ce9178;
}
.code-function {
color: #dcdcaa;
}
.code-number {
color: #b5cea8;
}
.code-variable {
color: #9cdcfe;
}
.code-property {
color: #4fc1ff;
}
/* Annotated Code */
.code-line {
position: relative;
padding-left: 10px;
}
.code-annotation {
position: absolute;
right: -300px;
background: var(--accent-purple);
padding: 5px 10px;
border-radius: 5px;
font-size: 0.8rem;
white-space: nowrap;
opacity: 0;
transition: opacity 0.3s ease;
}
.code-line:hover .code-annotation {
opacity: 1;
}
/* Steps */
.steps {
counter-reset: step-counter;
}
.step {
background: var(--bg-secondary);
border-radius: 15px;
padding: 30px;
margin: 20px 0;
position: relative;
padding-left: 80px;
transition: all 0.3s ease;
}
.step:hover {
background: var(--bg-card);
transform: translateX(10px);
}
.step::before {
counter-increment: step-counter;
content: counter(step-counter);
position: absolute;
left: 20px;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background: var(--gradient-server);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5rem;
font-weight: 700;
}
.step h3 {
color: var(--accent-orange);
margin-bottom: 10px;
}
/* Interactive Demo */
.demo-container {
background: var(--bg-secondary);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.server-visual {
background: #1e1e1e;
border-radius: 15px;
padding: 30px;
margin: 20px 0;
text-align: center;
}
.server-icon {
font-size: 4rem;
margin: 20px 0;
animation: serverPulse 2s infinite;
}
@keyframes serverPulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }
}
.server-status {
display: inline-block;
padding: 10px 20px;
border-radius: 20px;
font-weight: 600;
margin: 10px;
}
.server-status.offline {
background: rgba(239, 68, 68, 0.2);
color: var(--accent-red);
}
.server-status.online {
background: rgba(16, 185, 129, 0.2);
color: var(--accent-green);
}
.demo-button {
background: var(--gradient-server);
color: white;
border: none;
padding: 15px 40px;
border-radius: 50px;
font-size: 1.1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: inline-flex;
align-items: center;
gap: 10px;
margin: 10px;
}
.demo-button:hover {
transform: scale(1.05);
box-shadow: 0 10px 30px rgba(249, 115, 22, 0.4);
}
.demo-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Browser Mockup */
.browser-mockup {
background: #fff;
border-radius: 10px;
overflow: hidden;
margin: 20px 0;
box-shadow: var(--shadow);
}
.browser-header {
background: #e0e0e0;
padding: 10px;
display: flex;
align-items: center;
gap: 5px;
}
.browser-dot {
width: 12px;
height: 12px;
border-radius: 50%;
}
.browser-dot.red { background: #ff5f56; }
.browser-dot.yellow { background: #ffbd2e; }
.browser-dot.green { background: #27c93f; }
.browser-address {
flex: 1;
margin-left: 10px;
background: white;
padding: 5px 15px;
border-radius: 5px;
font-family: 'Fira Code', monospace;
font-size: 0.9rem;
color: #333;
}
.browser-content {
background: white;
padding: 40px;
min-height: 150px;
color: #333;
font-size: 1.5rem;
display: flex;
align-items: center;
justify-content: center;
}
/* Tabs */
.tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.tab {
padding: 12px 24px;
background: var(--bg-secondary);
border: none;
border-radius: 10px;
color: var(--text-primary);
cursor: pointer;
transition: all 0.3s ease;
font-size: 1rem;
}
.tab:hover {
background: var(--bg-card);
}
.tab.active {
background: var(--gradient-server);
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
animation: fadeIn 0.5s ease-out;
}
/* Accordion */
.accordion-header {
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background: var(--bg-secondary);
border-radius: 12px;
margin: 15px 0;
transition: all 0.3s ease;
}
.accordion-header:hover {
background: var(--bg-card);
}
.accordion-icon {
transition: transform 0.3s ease;
font-size: 1.2rem;
}
.accordion-icon.active {
transform: rotate(180deg);
}
.accordion-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease;
}
.accordion-content.active {
max-height: 2000px;
}
.accordion-body {
padding: 20px;
background: var(--bg-secondary);
border-radius: 12px;
margin-top: 5px;
}
/* Tips */
.tip {
background: rgba(59, 130, 246, 0.1);
border-left: 4px solid var(--accent-blue);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.tip::before {
content: '💡 ';
font-size: 1.5rem;
}
.warning {
background: rgba(239, 68, 68, 0.1);
border-left: 4px solid #ef4444;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.warning::before {
content: '⚠️ ';
font-size: 1.5rem;
}
.success {
background: rgba(16, 185, 129, 0.1);
border-left: 4px solid var(--accent-green);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.success::before {
content: '✅ ';
font-size: 1.5rem;
}
.info {
background: rgba(249, 115, 22, 0.1);
border-left: 4px solid var(--accent-orange);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.info::before {
content: '🔍 ';
font-size: 1.5rem;
}
/* Quiz */
.quiz-container {
background: var(--bg-card);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.quiz-question {
margin: 30px 0;
}
.quiz-question h3 {
color: var(--accent-purple);
margin-bottom: 20px;
}
.quiz-options {
display: flex;
flex-direction: column;
gap: 15px;
}
.quiz-option {
background: var(--bg-secondary);
padding: 20px;
border-radius: 12px;
cursor: pointer;
transition: all 0.3s ease;
border: 2px solid transparent;
}
.quiz-option:hover {
border-color: var(--accent-blue);
transform: translateX(10px);
}
.quiz-option.correct {
border-color: var(--accent-green);
background: rgba(16, 185, 129, 0.1);
}
.quiz-option.incorrect {
border-color: #ef4444;
background: rgba(239, 68, 68, 0.1);
}
.quiz-feedback {
margin-top: 15px;
padding: 15px;
border-radius: 10px;
display: none;
}
.quiz-feedback.show {
display: block;
animation: slideIn 0.5s ease-out;
}
@keyframes slideIn {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}
.quiz-feedback.correct {
background: rgba(16, 185, 129, 0.1);
border-left: 4px solid var(--accent-green);
}
.quiz-feedback.incorrect {
background: rgba(239, 68, 68, 0.1);
border-left: 4px solid #ef4444;
}
/* Cheat Sheet */
.cheat-sheet {
background: var(--gradient-server);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.cheat-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 30px;
}
.cheat-item {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
padding: 20px;
border-radius: 15px;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.cheat-item h4 {
color: #fff;
margin-bottom: 10px;
font-weight: 600;
}
.cheat-item code {
background: rgba(0, 0, 0, 0.3);
padding: 2px 8px;
border-radius: 5px;
font-size: 0.9rem;
}
.badge {
display: inline-block;
padding: 5px 15px;
background: var(--gradient-2);
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
margin: 5px;
}
.highlight {
color: var(--accent-orange);
font-weight: 600;
}
/* Footer */
.footer {
text-align: center;
padding: 40px 20px;
background: var(--bg-secondary);
margin-top: 80px;
}
.footer p {
color: var(--text-secondary);
margin: 10px 0;
}
/* Confetti */
.confetti {
position: fixed;
width: 10px;
height: 10px;
z-index: 9999;
}
@keyframes confetti-fall {
to {
transform: translateY(100vh) rotate(360deg);
opacity: 0;
}
}
/* Responsive */
@media (max-width: 768px) {
.hero h1 {
font-size: 2rem;
}
.command-display {
font-size: 0.9rem;
padding: 15px 20px;
}
.hero p {
font-size: 1rem;
}
.section-title {
font-size: 1.8rem;
}
.card {
padding: 25px;
}
.step {
padding-left: 70px;
}
.step::before {
width: 40px;
height: 40px;
font-size: 1.2rem;
}
.code-annotation {
display: none;
}
}
.diagram {
background: var(--bg-secondary);
border-radius: 15px;
padding: 30px;
margin: 30px 0;
text-align: center;
}
.diagram-flow {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
gap: 20px;
margin: 20px 0;
}
.diagram-box {
background: var(--bg-card);
padding: 20px;
border-radius: 12px;
min-width: 150px;
border: 2px solid var(--accent-blue);
}
.diagram-arrow {
font-size: 2rem;
color: var(--accent-green);
}
</style>
</head>
<body>
<div class="progress-container">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="hero">
<div class="hero-content">
<h1>🌐 Build Your First HTTP Server!</h1>
<div class="command-display">let http = require('http');<br>http.createServer(function (req, res) {<br> res.writeHead(200, {'Content-Type': 'text/html'});<br> res.end('Hello World!');<br>}).listen(8080);</div>
<p>Create a web server in just 5 lines of code! 🚀</p>
</div>
</div>
<div class="container">
<!-- What Is It Section -->
<section class="section" id="what-is-it">
<h2 class="section-title">🤔 What Is It?</h2>
<div class="card">
<p style="font-size: 1.2rem; margin-bottom: 20px;">
This code creates a <span class="highlight">web server</span> using Node.js - a program that listens for requests and sends back responses! 🎯
</p>
<p style="margin-bottom: 20px;">
Think of it as opening a restaurant: your server waits for customers (requests), takes their orders, and serves them food (responses). In this case, everyone gets "Hello World!" 🍽️
</p>
<div class="info">
<strong>In Simple Terms:</strong> This code makes your computer act like a website! When someone visits <code>http://localhost:8080</code>, they'll see "Hello World!" displayed in their browser. 🌟
</div>
<div class="diagram">
<h3 style="color: var(--accent-blue); margin-bottom: 20px;">How It Works 🔄</h3>
<div class="diagram-flow">
<div class="diagram-box">
<div style="font-size: 2rem; margin-bottom: 10px;">👤</div>
<strong>User's Browser</strong>
<p style="font-size: 0.9rem; margin-top: 5px;">Visits localhost:8080</p>
</div>
<div class="diagram-arrow">→</div>
<div class="diagram-box">
<div style="font-size: 2rem; margin-bottom: 10px;">🖥️</div>
<strong>Your Server</strong>
<p style="font-size: 0.9rem; margin-top: 5px;">Receives request</p>
</div>
<div class="diagram-arrow">→</div>
<div class="diagram-box">
<div style="font-size: 2rem; margin-bottom: 10px;">📤</div>
<strong>Response</strong>
<p style="font-size: 0.9rem; margin-top: 5px;">Sends "Hello World!"</p>
</div>
</div>
</div>
</div>
</section>
<!-- Why Use It Section -->
<section class="section" id="why-use-it">
<h2 class="section-title">✨ Why Use It?</h2>
<div class="card">
<div class="cheat-grid">
<div class="cheat-item">
<h4>🚀 Build Web Apps</h4>
<p>Create websites, APIs, and web applications from scratch!</p>
</div>
<div class="cheat-item">
<h4>⚡ Super Fast</h4>
<p>Node.js is incredibly fast and can handle thousands of requests!</p>
</div>
<div class="cheat-item">
<h4>🎯 Full Control</h4>
<p>You control every aspect of how your server responds!</p>
</div>
<div class="cheat-item">
<h4>🔧 Customizable</h4>
<p>Build exactly what you need without unnecessary bloat.</p>
</div>
<div class="cheat-item">
<h4>📚 Learn the Basics</h4>
<p>Understanding HTTP servers is fundamental to web development!</p>
</div>
<div class="cheat-item">
<h4>🌍 Real-World Skills</h4>
<p>This is how professional web servers work under the hood!</p>
</div>
</div>
</div>
</section>
<!-- How Does It Work Section -->
<section class="section" id="how-it-works">
<h2 class="section-title">⚙️ Line-by-Line Breakdown</h2>
<div class="card">
<p style="margin-bottom: 30px;">Let's dissect each line of code! 🔬</p>
<div class="steps">
<div class="step">
<h3>Line 1: Import the HTTP Module</h3>
<div class="code-block">
<pre><code><span class="code-keyword">let</span> <span class="code-variable">http</span> = <span class="code-function">require</span>(<span class="code-string">'http'</span>);</code></pre>
</div>
<p>This loads Node.js's built-in HTTP module, which gives us the tools to create a web server. Think of it like importing a toolbox! 🧰</p>
</div>
<div class="step">
<h3>Line 2: Create the Server</h3>
<div class="code-block">
<pre><code><span class="code-variable">http</span>.<span class="code-function">createServer</span>(<span class="code-keyword">function</span> (<span class="code-variable">req</span>, <span class="code-variable">res</span>) {</code></pre>
</div>
<p><code>createServer()</code> creates a new server. The function receives two parameters:</p>
<ul style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li><code>req</code> (request): Information about what the user wants</li>
<li><code>res</code> (response): How we send data back to the user</li>
</ul>
</div>
<div class="step">
<h3>Line 3: Set Response Headers</h3>
<div class="code-block">
<pre><code> <span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});</code></pre>
</div>
<p>This sets the response headers:</p>
<ul style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li><code>200</code>: HTTP status code meaning "OK, everything worked!"</li>
<li><code>'Content-Type': 'text/html'</code>: Tells the browser we're sending HTML</li>
</ul>
</div>
<div class="step">
<h3>Line 4: Send the Response</h3>
<div class="code-block">
<pre><code> <span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Hello World!'</span>);</code></pre>
</div>
<p><code>res.end()</code> sends the response to the user and closes the connection. This is what appears in the browser! 📤</p>
</div>
<div class="step">
<h3>Line 5: Start Listening</h3>
<div class="code-block">
<pre><code>}).<span class="code-function">listen</span>(<span class="code-number">8080</span>);</code></pre>
</div>
<p><code>listen(8080)</code> tells the server to start listening on port 8080. Now your server is live and ready to accept requests! 🎉</p>
</div>
</div>
<div class="success" style="margin-top: 30px;">
<strong>Pro Tip:</strong> Port 8080 is commonly used for development. In production, web servers typically use port 80 (HTTP) or 443 (HTTPS)!
</div>
</div>
</section>
<!-- Complete Code with Annotations -->
<section class="section" id="code-breakdown">
<h2 class="section-title">💻 Complete Annotated Code</h2>
<div class="card">
<div class="code-block">
<div class="code-header">
<span class="code-lang">server.js</span>
<button class="copy-btn" onclick="copyCode(this, 'full-code')">📋 Copy</button>
</div>
<pre><code id="full-code"><span class="code-comment">// Import the HTTP module</span>
<span class="code-keyword">let</span> <span class="code-variable">http</span> = <span class="code-function">require</span>(<span class="code-string">'http'</span>);
<span class="code-comment">// Create a server that responds to all requests</span>
<span class="code-variable">http</span>.<span class="code-function">createServer</span>(<span class="code-keyword">function</span> (<span class="code-variable">req</span>, <span class="code-variable">res</span>) {
<span class="code-comment">// Set the response header (status 200 = OK)</span>
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-comment">// Send the response and close the connection</span>
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Hello World!'</span>);
}).<span class="code-function">listen</span>(<span class="code-number">8080</span>); <span class="code-comment">// Listen on port 8080</span>
<span class="code-comment">// Optional: Log a message to confirm server is running</span>
console.<span class="code-function">log</span>(<span class="code-string">'Server running at http://localhost:8080/'</span>);</code></pre>
</div>
<div class="tip" style="margin-top: 30px;">
<strong>How to Run:</strong>
<ol style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li>Save this code as <code>server.js</code></li>
<li>Open terminal in the same directory</li>
<li>Run <code>node server.js</code></li>
<li>Visit <code>http://localhost:8080</code> in your browser!</li>
</ol>
</div>
</div>
</section>
<!-- Interactive Demo -->
<section class="section" id="demo">
<h2 class="section-title">🎮 Interactive Visualization</h2>
<div class="demo-container">
<h3 style="margin-bottom: 20px;">See Your Server in Action! 🎯</h3>
<p style="margin-bottom: 30px;">Click the button to simulate starting your server and making a request!</p>
<div class="server-visual">
<div class="server-icon" id="serverIcon">🖥️</div>
<div class="server-status offline" id="serverStatus">Server Offline</div>
<div style="margin: 30px 0;">
<button class="demo-button" id="startServerBtn" onclick="startServer()">
<span>▶️</span>
<span>Start Server</span>
</button>
<button class="demo-button" id="makeRequestBtn" onclick="makeRequest()" disabled>
<span>📡</span>
<span>Make Request</span>
</button>
</div>
</div>
<div id="browserMockup" style="display: none;">
<div class="browser-mockup">
<div class="browser-header">
<div class="browser-dot red"></div>
<div class="browser-dot yellow"></div>
<div class="browser-dot green"></div>
<div class="browser-address">http://localhost:8080</div>
</div>
<div class="browser-content" id="browserContent">
<div style="color: #999;">Waiting for response...</div>
</div>
</div>
</div>
</div>
</section>
<!-- Variations Section -->
<section class="section" id="variations">
<h2 class="section-title">🎨 Code Variations</h2>
<div class="card">
<p style="margin-bottom: 30px;">Here are different ways to write the same server! 🔄</p>
<div class="tabs">
<button class="tab active" onclick="switchTab('arrow')">Arrow Function</button>
<button class="tab" onclick="switchTab('async')">Async/Await</button>
<button class="tab" onclick="switchTab('express')">Using Express</button>
</div>
<div id="arrow" class="tab-content active">
<h3 style="color: var(--accent-green); margin-bottom: 15px;">Modern ES6 Arrow Function</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">server.js</span>
<button class="copy-btn" onclick="copyCode(this, 'arrow-code')">📋 Copy</button>
</div>
<pre><code id="arrow-code"><span class="code-keyword">const</span> <span class="code-variable">http</span> = <span class="code-function">require</span>(<span class="code-string">'http'</span>);
<span class="code-keyword">const</span> <span class="code-variable">server</span> = <span class="code-variable">http</span>.<span class="code-function">createServer</span>((<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Hello World!'</span>);
});
<span class="code-variable">server</span>.<span class="code-function">listen</span>(<span class="code-number">8080</span>, () => {
console.<span class="code-function">log</span>(<span class="code-string">'Server running on port 8080'</span>);
});</code></pre>
</div>
<p style="margin-top: 15px;">✨ Uses modern JavaScript syntax with arrow functions and a callback for when the server starts!</p>
</div>
<div id="async" class="tab-content">
<h3 style="color: var(--accent-green); margin-bottom: 15px;">With Request Handling</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">server.js</span>
<button class="copy-btn" onclick="copyCode(this, 'async-code')">📋 Copy</button>
</div>
<pre><code id="async-code"><span class="code-keyword">const</span> <span class="code-variable">http</span> = <span class="code-function">require</span>(<span class="code-string">'http'</span>);
<span class="code-keyword">const</span> <span class="code-variable">server</span> = <span class="code-variable">http</span>.<span class="code-function">createServer</span>((<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-comment">// Check the URL path</span>
<span class="code-keyword">if</span> (<span class="code-variable">req</span>.<span class="code-property">url</span> === <span class="code-string">'/'</span>) {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'<h1>Home Page</h1>'</span>);
} <span class="code-keyword">else if</span> (<span class="code-variable">req</span>.<span class="code-property">url</span> === <span class="code-string">'/about'</span>) {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'<h1>About Page</h1>'</span>);
} <span class="code-keyword">else</span> {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">404</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'<h1>404 Not Found</h1>'</span>);
}
});
<span class="code-variable">server</span>.<span class="code-function">listen</span>(<span class="code-number">8080</span>);</code></pre>
</div>
<p style="margin-top: 15px;">🎯 This version handles different URL paths and returns different content!</p>
</div>
<div id="express" class="tab-content">
<h3 style="color: var(--accent-green); margin-bottom: 15px;">Using Express Framework</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">server.js</span>
<button class="copy-btn" onclick="copyCode(this, 'express-code')">📋 Copy</button>
</div>
<pre><code id="express-code"><span class="code-comment">// First: npm install express</span>
<span class="code-keyword">const</span> <span class="code-variable">express</span> = <span class="code-function">require</span>(<span class="code-string">'express'</span>);
<span class="code-keyword">const</span> <span class="code-variable">app</span> = <span class="code-function">express</span>();
<span class="code-variable">app</span>.<span class="code-function">get</span>(<span class="code-string">'/'</span>, (<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-variable">res</span>.<span class="code-function">send</span>(<span class="code-string">'Hello World!'</span>);
});
<span class="code-variable">app</span>.<span class="code-function">listen</span>(<span class="code-number">8080</span>, () => {
console.<span class="code-function">log</span>(<span class="code-string">'Server running on port 8080'</span>);
});</code></pre>
</div>
<p style="margin-top: 15px;">🚀 Express makes it even easier! This is what most developers use in production.</p>
</div>
</div>
</section>
<!-- Common Mistakes Section -->
<section class="section" id="mistakes">
<h2 class="section-title">❌ Common Mistakes</h2>
<div class="card">
<div class="accordion">
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>1. Forgetting to Call res.end()</strong></span>
<span class="accordion-icon">▼</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Problem:</strong> The browser will hang forever waiting for a response!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// ❌ Wrong - no res.end()</span>
<span class="code-variable">http</span>.<span class="code-function">createServer</span>((<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>);
<span class="code-comment">// Oops! Forgot res.end()</span>
});
<span class="code-comment">// ✅ Correct</span>
<span class="code-variable">http</span>.<span class="code-function">createServer</span>((<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>);
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Done!'</span>); <span class="code-comment">// Always end the response!</span>
});</code></pre>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>2. Port Already in Use</strong></span>
<span class="accordion-icon">▼</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Error:</strong> "EADDRINUSE: address already in use"
</div>
<p style="margin-top: 15px;"><strong>Cause:</strong> Another program is already using port 8080!</p>
<p style="margin-top: 10px;"><strong>Solutions:</strong></p>
<ul style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li>Use a different port: <code>.listen(3000)</code></li>
<li>Stop the other server first</li>
<li>Find and kill the process using the port</li>
</ul>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>3. Not Handling Errors</strong></span>
<span class="accordion-icon">▼</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Problem:</strong> Server crashes on errors!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// ✅ Better: Handle errors</span>
<span class="code-keyword">const</span> <span class="code-variable">server</span> = <span class="code-variable">http</span>.<span class="code-function">createServer</span>((<span class="code-variable">req</span>, <span class="code-variable">res</span>) => {
<span class="code-keyword">try</span> {
<span class="code-variable">res</span>.<span class="code-function">writeHead</span>(<span class="code-number">200</span>, {<span class="code-string">'Content-Type'</span>: <span class="code-string">'text/html'</span>});
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Hello World!'</span>);
} <span class="code-keyword">catch</span> (<span class="code-variable">error</span>) {
console.<span class="code-function">error</span>(<span class="code-string">'Error:'</span>, <span class="code-variable">error</span>);
<span class="code-variable">res</span>.<span class="code-function">end</span>(<span class="code-string">'Server Error'</span>);
}
});
<span class="code-variable">server</span>.<span class="code-function">on</span>(<span class="code-string">'error'</span>, (<span class="code-variable">err</span>) => {
console.<span class="code-function">error</span>(<span class="code-string">'Server error:'</span>, <span class="code-variable">err</span>);
});</code></pre>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>4. Wrong Content-Type</strong></span>
<span class="accordion-icon">▼</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Problem:</strong> Browser doesn't display content correctly!
</div>
<div class="tip" style="margin-top: 15px;">
<strong>Common Content-Types:</strong>
<ul style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li><code>'text/html'</code> - HTML pages</li>
<li><code>'application/json'</code> - JSON data</li>
<li><code>'text/plain'</code> - Plain text</li>
<li><code>'text/css'</code> - CSS files</li>
<li><code>'application/javascript'</code> - JavaScript files</li>
</ul>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>5. Not Stopping the Server</strong></span>
<span class="accordion-icon">▼</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Problem:</strong> Server keeps running in the background!
</div>
<p style="margin-top: 15px;"><strong>How to Stop:</strong></p>
<ul style="margin-left: 30px; margin-top: 10px; line-height: 2;">
<li>Press <code>Ctrl + C</code> in the terminal</li>
<li>Close the terminal window</li>
<li>Use <code>server.close()</code> in code</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- Pro Tips Section -->
<section class="section" id="pro-tips">
<h2 class="section-title">🚀 Pro Tips</h2>
<div class="card">
<div class="cheat-grid">
<div class="cheat-item">
<h4>📝 Add Logging</h4>
<p>Log every request: <code>console.log(req.url, req.method)</code></p>
</div>
<div class="cheat-item">
<h4>🔄 Auto-Restart</h4>
<p>Use <code>nodemon</code> to auto-restart on file changes!</p>
</div>
<div class="cheat-item">
<h4>🌐 Environment Variables</h4>
<p>Use <code>process.env.PORT || 8080</code> for flexible ports!</p>
</div>
<div class="cheat-item">
<h4>📊 Status Codes</h4>
<p>Learn HTTP codes: 200 (OK), 404 (Not Found), 500 (Error)</p>
</div>
<div class="cheat-item">
<h4>🔒 Security</h4>
<p>Never expose sensitive data in responses!</p>
</div>
<div class="cheat-item">
<h4>⚡ Performance</h4>
<p>Use clustering for better performance with multiple cores!</p>
</div>
</div>
<div class="success" style="margin-top: 30px;">
<strong>🎓 Next Steps:</strong> Learn about Express.js, routing, middleware, and building REST APIs!
</div>
</div>
</section>
<!-- Quiz Section -->
<section class="section" id="quiz">
<h2 class="section-title">🎯 Knowledge Check Quiz</h2>
<div class="quiz-container">
<p style="margin-bottom: 30px;">Test your understanding! Click on the correct answer. 🧠</p>
<div class="quiz-question">
<h3>Question 1: What does res.end() do?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
A) Stops the server
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 1)">
B) Sends the response and closes the connection
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
C) Deletes the server
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
D) Restarts the request
</div>
</div>
<div class="quiz-feedback" id="feedback1"></div>
</div>
<div class="quiz-question">
<h3>Question 2: What does the number 8080 represent in .listen(8080)?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
A) The server ID
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 2)">
B) The port number the server listens on
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
C) The maximum number of connections
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
D) The timeout duration
</div>
</div>
<div class="quiz-feedback" id="feedback2"></div>
</div>
<div class="quiz-question">
<h3>Question 3: What does the status code 200 mean?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
A) Server error
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
B) Page not found
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 3)">
C) Success - everything is OK
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
D) Redirect to another page
</div>
</div>
<div class="quiz-feedback" id="feedback3"></div>
</div>
<div id="quizScore" style="margin-top: 30px; padding: 20px; background: var(--bg-secondary); border-radius: 12px; display: none;">
<h3 style="color: var(--accent-green);">🎉 Quiz Complete!</h3>
<p id="scoreText" style="font-size: 1.2rem; margin-top: 10px;"></p>
</div>
</div>
</section>
<!-- Cheat Sheet Section -->
<section class="section" id="cheat-sheet">
<h2 class="section-title">📚 Quick Reference Cheat Sheet</h2>
<div class="cheat-sheet">
<h3 style="margin-bottom: 20px;">Save this for later! 🔖</h3>
<div class="cheat-grid">
<div class="cheat-item">
<h4>📦 Import Module</h4>
<p><code>require('http')</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Load HTTP module</p>
</div>
<div class="cheat-item">
<h4>🏗️ Create Server</h4>
<p><code>http.createServer(callback)</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Initialize server</p>
</div>
<div class="cheat-item">
<h4>📝 Write Header</h4>
<p><code>res.writeHead(200, {...})</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Set response headers</p>
</div>
<div class="cheat-item">
<h4>📤 Send Response</h4>
<p><code>res.end('content')</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Send and close</p>
</div>
<div class="cheat-item">
<h4>👂 Listen</h4>
<p><code>.listen(port)</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Start listening</p>
</div>
<div class="cheat-item">
<h4>🌐 Access</h4>
<p><code>localhost:8080</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Visit in browser</p>
</div>
</div>
<div style="margin-top: 30px; padding: 20px; background: rgba(255,255,255,0.1); border-radius: 12px;">
<h4 style="color: #fff; margin-bottom: 15px;">🎯 Complete Template</h4>
<div class="code-block">
<pre><code><span class="code-keyword">const</span> <span class="code-variable">http</span> = <span class="code-function">require</span>(<span class="code-string">'http
Live Preview