0% found this document useful (0 votes)
3 views23 pages

Node.js HTTP Module Basics

The HTTP module in Node.js enables the creation of web servers and the handling of HTTP requests and responses. It allows developers to serve content, manage routes, and respond to client requests using the HTTP protocol. Key functionalities include creating a server, handling different URLs, and reading query parameters, all without needing additional installations.

Uploaded by

grozasvar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Node.js HTTP Module Basics

The HTTP module in Node.js enables the creation of web servers and the handling of HTTP requests and responses. It allows developers to serve content, manage routes, and respond to client requests using the HTTP protocol. Key functionalities include creating a server, handling different URLs, and reading query parameters, all without needing additional installations.

Uploaded by

grozasvar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Introduction to HTTP

Module in [Link]
What is the HTTP Module?

 The HTTP module in [Link] allows you to create web servers


and handle HTTP requests and responses.

It helps [Link] interact with web clients (like browsers) using the
HTTP protocol — the same protocol used for web communication.
Why We Use It

 To create a server that listens to client requests.



To send responses such as HTML pages, JSON data, or text.

To handle different URLs or routes.

To build the backend of web applications.
Importing the Module

 The HTTP module is a core module — no installation needed.

 const http = require('http’);


Creating a Basic Server
 const http = require('http');

 // Create a server
 const server = [Link]((req, res) => {
 [Link](200, { 'Content-Type': 'text/plain' });
 [Link]('Hello, welcome to my [Link] server!');
 });

 // Server listens on port 3000


 [Link](3000, () => {
 [Link]('Server is running on [Link]
 });
Handling Routes
 You can respond differently based on the URL path:
 const http = require('http');

 const server = [Link]((req, res) => {


 if ([Link] === '/') {
 [Link](200, { 'Content-Type': 'text/html' });
 [Link]('<h1>Home Page</h1>');
 } else if ([Link] === '/about') {
 [Link](200, { 'Content-Type': 'text/html' });
 [Link]('<h1>About Us Page</h1>');
 } else {
 [Link](404, { 'Content-Type': 'text/html' });
 [Link]('<h1>404 - Page Not Found</h1>');
 }
 });

 [Link](3000, () => {
 [Link]('Server running at [Link]
 });
Common Methods

Method Description
createServer() Creates an HTTP server
listen() Binds the server to a specific port
writeHead() Sends status code and headers
end() Ends the response
[Link] Gets the requested URL path
Gets HTTP method (GET, POST,
[Link]
etc.)
 const http = require('http’);

 This imports [Link]’s built-in HTTP module.


The http module allows you to create a web server that can send
and receive data over the HTTP protocol (like browsers do).
 No need to install — it’s built into [Link].
Create a Server

 const server = [Link]((req, res) => {


 [Link](200, { 'Content-Type': 'text/plain' });
 [Link]('Hello, welcome to my [Link] server!');
 });
Inside the Callback:

 [Link](200, { 'Content-Type': 'text/plain' });


 [Link](200, {...}) → sends a status code and headers.
 200 means OK (successful response).
 { 'Content-Type': 'text/plain' } tells the browser that the response is
plain text (not HTML or JSON).

 [Link]('Hello, welcome to my [Link] server!');


Start the Server

 [Link](3000, () => {
 [Link]('Server is running on [Link]
 });
Case 1: Home Page
 if ([Link] === '/') {
 [Link](200, { 'Content-Type': 'text/html' });
 [Link]('<h1>Home Page</h1>');
 }
Case 2: About Page

 else if ([Link] === '/about') {


 [Link](200, { 'Content-Type': 'text/html' });
 [Link]('<h1>About Us Page</h1>');
 }
Case 3: Any Other Page (404
Error)
 else {
 [Link](404, { 'Content-Type': 'text/html' });
 [Link]('<h1>404 - Page Not Found</h1>');
 }
Starting the Server

 [Link](3000, () => {
 [Link]('Server running at [Link]
 });
3. Serving an HTML File
 const http = require('http');

 const fs = require('fs');

 const server = [Link]((req, res) => {

 if ([Link] === '/') {

 [Link]('[Link]', (err, data) => {

 if (err) {

 [Link](500);

 [Link]('Error loading HTML file');

 } else {

 [Link](200, {'Content-Type': 'text/html'});

 [Link](data);

 }

 });

 } else {

 [Link](404, {'Content-Type': 'text/plain'});

 [Link]('Page Not Found');

 }

 });

 [Link](3000, () => [Link]('Server running at [Link]


Common Status Codes in Your Code

Status Code Meaning When It’s Used


The request was
successful, and the
200 OK ✅ Success server is returning the
requested data (like
your HTML file).
The requested page or
404 Not Found 🚫 Resource Missing file doesn’t exist on
the server.
Something went
wrong inside the
500 Internal Server server — e.g., the
💥 Server-Side Error
Error HTML file failed to
load because of a file
system error.
4. Reading Query Parameters (Using
URL module)
 const http = require('http');
 const url = require('url');

 const server = [Link]((req, res) => {


 const queryObject = [Link]([Link], true).query;

 [Link](200, {'Content-Type': 'text/plain'});


 [Link](`Hello, ${[Link] || 'Guest'}!`);
 });

 [Link](3000, () => [Link]('Server running at [Link]


 1. const http = require('http’);

 const url = require('url’);

 [Link]((req, res) => { ... })


 const queryObject = [Link]([Link], true).query;

 [Link] might look like this when you visit the server:
 [Link]([Link], true) converts that URL string into an object,
separating its parts:
 The .query property gives you the query parameters as an object:
 [Link](200, {'Content-Type': 'text/plain’});

 [Link](\Hello, ${[Link] || 'Guest'}!`);`


 [Link](3000, ...)
5. Handling POST Requests
(Reading Request Body)

You might also like