Build a Node.js HTTP Server From Scratch (No Frameworks Needed and Less Then 30 Lines!)
Source: Dev.to
Introduction
Most Node.js tutorials jump straight to Express. Before you reach for a framework, it’s worth understanding what’s happening underneath—because once you do, everything else clicks.
In this tutorial, Deividas Strole walks you through building a simple HTTP server using only Node’s built‑in modules. No Express, no npm install, no dependencies of any kind. Just the runtime doing its thing.
By the end, you’ll have a working local server that serves a static HTML file—and a much clearer mental model of how the web actually works.
What You’ll Learn
- How the Node.js
httpmodule handles requests and responses - How to read files from disk using the
fsmodule - How to serve a static HTML page
- How to handle 404 routes cleanly
- How to run everything locally in under a minute
Prerequisites
- Node.js installed (nodejs.org)
- Basic JavaScript knowledge
- A text editor (VS Code, Vim, anything works)
No package manager needed. We’re going fully built‑in.
Step 1: Create the HTML File
Create index.html. This is what your server will send to the browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Node Server</title>
<style>
body {
font-family: monospace;
background: #0d1117;
color: #e6edf3;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}
h1 { color: #3fb950; }
p { color: #8b949e; }
</style>
</head>
<body>
<h1>✓ Server is running</h1>
<p>Served by Node.js — no frameworks needed.</p>
</body>
</html>Step 2: Create the Server
Create server.js. This is where the magic happens.
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 3000;
const HTML_FILE = path.join(__dirname, 'index.html');
const server = http.createServer((req, res) => {
// Only serve the root route
if (req.url === '/' || req.url === '/index.html') {
fs.readFile(HTML_FILE, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
} else {
// Everything else gets a 404
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
server.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});Step 3: Run It
In your terminal, navigate to the project folder and run:
node server.jsOpen your browser and go to http://localhost:3000. You’ll see the HTML page served by your own server.
Visiting any other path returns a 404 Not Found response.
To stop the server, press Ctrl + C.
Full Source Code
The complete project is on GitHub:
👉 (drop a ⭐ if you found it useful)