Frameless Web Server Setup with Node.js HTTP Module
In this guide, you'll learn how to build a web server from scratch using Node.js' built-in http module.
We will develop a fast and micro server running entirely on the Node.js core, without frameworks like Express.
Each of the steps below is prepared with detailed explanations and code examples.
🧱 Step 1 – Creating the Project Folder
First, let's create the folder where we will store our server files.
mkdir ilk-sunucular
cd ilk-sunucular
touch temel-sunucu.js
This step allows you to keep your project organized.
🧩 Step 2 – Writing the Basic HTTP Server
The code below is a minimal HTTP server that listens for all incoming requests and returns a single text.
const http = require("http");
const host = 'localhost';
const port = 8000;
const istekDinleyici = function (istek, yanit) {
yanit.writeHead(200);
yanit.end("GenixNode'taki ilk sunucum!");
};
const sunucu = http.createServer(istekDinleyici);
sunucu.listen(port, host, () => {
console.log(`Sunucu aktif: http://${host}:${port}`);
});
Things you learned in this step: How http.createServer() works
Basic logic of req and res objects
How the presenter starts listening
🟦 Step 3 – Running and Testing the Server
Start the server:
node temel-sunucu.js
Output:
Sunucu aktif: http://localhost:8000
To test:
curl http://localhost:8000
📄 Step 4 – Serving JSON Content
The most common content type when making an API is JSON.
yanit.setHeader("Content-Type", "application/json");
yanit.writeHead(200);
const veri = {
mesaj: "Bu bir JSON yanıtıdır.",
zamanDamgasi: Date.now(),
sunucu: "tr1-node01"
};
yanit.end(JSON.stringify(veri));
Why is it important? JSON is the foundation of API standards.
If the Content-Type is not correct, the browser and clients will process the response incorrectly.
🟥 Step 5 – Presenting HTML Content
yanit.setHeader("Content-Type", "text/html");
yanit.writeHead(200);
yanit.end(`
<html>
<body>
<h1>GenixNode HTML Denemesi</h1>
<p>Node.js ile HTML çıktısı gönderiyoruz.</p>
</body>
</html>
`);
In this step: You learned how to create an HTML output viewable in the browser.
🟧 Step 6 – Submitting CSV Content
yanit.setHeader("Content-Type", "text/csv");
yanit.setHeader("Content-Disposition", "attachment;filename=rapor.csv");
yanit.writeHead(200);
yanit.end(`id,urun,stok\n1,Sunucu Örneği,50\n2,Veritabani,12`);
File download logic: Content-Disposition header triggers download
CSV is especially used in reporting
📂 Step 7 – Serving HTML Page from File (Realistic Scenario)
Let's create an HTML file:
touch ana-sayfa.html
ana-sayfa.html
<!DOCTYPE html>
<html lang="tr">
<head>
<title>GenixNode Sunucusu</title>
<meta charset="UTF-8" />
<style>
body { background-color: #ff9933; text-align: center; padding-top: 50px; }
</style>
</head>
<body>
<h1>Tebrikler! Dosyadan HTML sunumu başarılı.</h1>
</body>
</html>
Server side:
const http = require("http");
const fs = require("fs");
const host = 'localhost';
const port = 8000;
const istekDinleyici = function (istek, yanit) {
yanit.setHeader("Content-Type", "text/html");
fs.readFile(__dirname + "/ana-sayfa.html", (hata, veri) => {
if (hata) {
yanit.writeHead(500);
return yanit.end("Hata: HTML dosyası okunamadı.");
}
yanit.writeHead(200);
yanit.end(veri);
});
};
const sunucu = http.createServer(istekDinleyici);
sunucu.listen(port, host, () => {
console.log(`Sunucu aktif: http://${host}:${port}`);
});
Learnings here: Reading files with fs.readFile()
asynchronous structure
How to issue 500 Internal Server Error
❓ FAQ – Frequently Asked Questions
1. Why should I use the http module instead of Express?
Ideal for developing lightweight and fast servers without dependencies.
2. How to make a public server?
host → "0.0.0.0"
3. What is the difference between writeHead() and setHeader()?
setHeader: adds independent header
writeHead: batch sends status code + headers
4. How do I resolve the “EADDRINUSE” error?
lsof -i :8000
Close the process using the port or change the port.
🎯 Result
With this guide:
You have set up a basic HTTP server
You sent JSON, HTML, CSV responses
You learned to serve HTML from file
You now know the basics of the Node.js HTTP module at a professional level.

