Creating a Simple Web Server in Node.js
⏱ Estimated reading time: 2 min
A web server is a program that receives requests from clients (browsers) and sends back responses.
Node.js makes this easy using its built-in http module, without any external libraries.
1. Import the HTTP Module
The http module allows Node.js to handle web requests and responses.
2. Create the Server
Use createServer() to define how the server should respond to requests.
Here:
-
requestcontains client information -
responseis used to send data back -
200indicates a successful request
3. Start Listening on a Port
A port is a communication endpoint. The server waits for requests on a specific port.
4. Complete Code Example
5. Running the Server
-
Save the file as
server.js -
Open terminal and run:
6. Why This Server Works Well
-
Uses non-blocking I/O
-
Handles multiple requests efficiently
-
Requires no external packages
-
Lightweight and fast
7. Use Cases
-
Learning Node.js basics
-
Testing APIs
-
Simple backend services
8. Key Takeaway
With just a few lines of code, Node.js can act as a fully functional web server.
This simplicity, combined with high performance, makes Node.js ideal for building scalable network applications.
Open a browser and visit:
Register Now
Share this Post
← Back to Tutorials