Creating a Web Server with Node.js HTTP Module
What will you learn in this guide?
In this guide, you will create a web server from scratch with Node.js' built-in HTTP module.
You will see routing, JSON/HTML responses, static file serving and basic security steps.
What is HTTP Module?
The HTTP module is the core component of Node.js.
It allows you to create an HTTP server without requiring additional packages.
Advantages:
- No framework
- Low memory usage
- Direct control of HTTP logic
Prerequisites
- Node.js 20 or above
- Basic JavaScript knowledge
- Terminal usage
1. Creating a Simple HTTP Server
In this step, a simple server that everyone can access is set up.
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end("Merhaba Node.js");
});
server.listen(8000);
- This code creates a simple HTTP server running on port 8000.
2. Returning JSON Response
- You need JSON response to develop API.
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(JSON.stringify({ status: "ok" }));
- This structure allows the client to process the response as JSON.
3. HTML Content Rotation
- HTML response is returned for browsers.
res.setHeader("Content-Type", "text/html");
res.writeHead(200);
res.end("<h1>Merhaba</h1>");
- HTML content is written directly into the response body.
4. Serving HTML from File
- HTML files can be read from disk.
const fs = require("fs");
fs.readFile("index.html", (err, data) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
- This method maintains code order in large projects.
5. Routing (URL Management)
- Different responses are generated for different URLs.
if (req.url === "/api") {
res.end("API endpoint");
} else {
res.writeHead(404);
res.end("Bulunamadı");
}
- This structure provides routing without a framework.
6. Reading POST Requests
- POST data arrives piecemeal.
let body = "";
req.on("data", chunk => body += chunk);
req.on("end", () => {
const data = JSON.parse(body);
});
- JSON should not be parsed before all data arrives.
7. Static File Serving
- Static structure is required for CSS, JS and images.
const path = require("path");
const filePath = path.resolve("public", "." + req.url);
- This check prevents directory traversal attacks.
8. Error Management
- The server should not crash.
res.writeHead(500);
res.end("Sunucu hatası");
- Internal error details should not be shown to the user.
9. Using ES Module (import)
- ES Module is preferred in modern projects.
import http from "http";
For ESM usage, "type": "module" is added to package.json.
Suggestions for Production
-
Use Nginx + HTTPS instead of HTTP
-
Manage processes with PM2
-
Cache static files
-
Use stream for large files
Frequently Asked Questions (FAQ)
1. Is the HTTP module suitable for production? Yes. Ideal for microservice and lightweight APIs.
2. Why HTTP instead of Express? Provides less dependency and full control.
3. Is it safe to serve static files? Yes, it is safe if the correct path is checked.
4. Why does the POST body come in pieces? Node.js works stream-based.
5. Which port should I use? 8000, 8080 or 3000 are common.
Result
With this guide, you can create framework-free, controlled and high-performance servers using the Node.js HTTP module.
When going live, you can safely publish your Node.js projects on the GenixNode infrastructure.

