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:
Open browser:
β
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
| Feature | Description |
|---|---|
| Built-in module | No installation needed |
| Create servers | Handle HTTP requests/responses |
| Supports routing | GET, POST, etc. |
| Send text/HTML/JSON | Easy response handling |
| Handle files | Serve static content |
| Forms backend | Foundation for Express.js |
