Node.js HTTP Module

🌐 Node.js – HTTP Module

The HTTP module in Node.js allows you to create web servers and handle HTTP requests and responses.
It is a built-in module, so you don’t need to install anything.


βœ… 1. Import the HTTP Module



 


βœ… 2. Create a Simple HTTP Server


 

βœ” Run:

node server.js

Open browser:

http://localhost:3000

βœ… 3. Understanding Request (req) and Response (res)

req object contains:

  • URL (req.url)

  • HTTP method (req.method)

  • Request headers (req.headers)

  • Query params

  • Body data

res object is used to send:

  • Status codes

  • Headers

  • HTML / JSON / Text output


βœ… 4. Sending HTML Response



 


βœ… 5. Sending JSON Response



 


βœ… 6. Handling Different Routes


 


βœ… 7. Handling Methods (GET, POST, etc.)


 


βœ… 8. Serving Static Files (HTML, CSS, JS)


 


βœ… 9. HTTP Status Codes Example



 


βœ… 10. Complete Example Server


 


🎯 Summary

FeatureDescription
Built-in moduleNo installation needed
Create serversHandle HTTP requests/responses
Supports routingGET, POST, etc.
Send text/HTML/JSONEasy response handling
Handle filesServe static content
Forms backendFoundation for Express.js

You may also like...