How to Create an HTTP Server in Node.js From Scratch
Creating an HTTP server is the first step of backend development. Here is how to do it in Node.js.
How to Create an HTTP Server in Node.js From Scratch
Creating an HTTP server is the first step of Node.js backend development. Here is how to do it from scratch.
Use the Built-in http Module
Node.js has a built-in http module for creating servers. Import it with require('http'), call http.createServer with a request listener, and listen on a port. This is the foundation before Express.
The Request Listener
The listener is a function that receives request and response. The request has the URL, method, and headers. The response is what you send back: status code, headers, and body.
Sending a Response
Call response.writeHead with a status code and headers, then response.end with the body. For JSON APIs, set Content-Type to application/json and send a JSON string.
Handling Different Routes
Check request.url and request.method to handle different routes. If url is '/api/users' and method is 'GET', send the users. This is manual routing, which Express makes easier, but it teaches how servers work.
Starting the Server
Call server.listen(port) to start listening on a port. Visit http://localhost:3000 in a browser to see your server respond.
Why Start With the http Module
Building a server with the raw http module teaches how request handling, routing, and responses work. Express abstracts this, but understanding the raw module makes Express more meaningful.
The Takeaway
Create an HTTP server with require('http'), http.createServer with a request listener, send responses with writeHead and end, handle routes by checking url and method, and listen on a port. This is the foundation of Node.js backend development.
Import the http module with require('http'), call http.createServer with a request listener function, and listen on a port with server.listen(port). The listener receives request and response objects.
A function that receives request and response. The request has the URL, method, and headers. The response is what you send back: status code, headers, and body via writeHead and end.
Call response.writeHead(200, { 'Content-Type': 'application/json' }), then response.end(JSON.stringify(data)). Set the Content-Type to application/json and send a JSON string.
Start with the http module to understand how servers work, then use Express for productivity. Express handles routing, middleware, and request parsing, but understanding the raw module first makes Express more meaningful.
Check request.url and request.method in the listener. If url is '/api/users' and method is 'GET', send the users. This is manual routing, which Express makes easier, but it teaches how servers work.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

