0% found this document useful (0 votes)
11 views27 pages

Session 04-Node and HTTP

The document outlines the essentials of networking and the implementation of a simple HTTP server using Node.js. It covers client-server communication, HTTP protocols, and the use of Node's HTTP, path, and fs modules to serve static HTML files. The document provides step-by-step instructions for setting up the server and testing it with Postman.

Uploaded by

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

Session 04-Node and HTTP

The document outlines the essentials of networking and the implementation of a simple HTTP server using Node.js. It covers client-server communication, HTTP protocols, and the use of Node's HTTP, path, and fs modules to serve static HTML files. The document provides step-by-step instructions for setting up the server and testing it with Postman.

Uploaded by

nguyenldhe176088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Node and HTTP

Objectives
◆ Understand the Networking Essentials
◆ Create and implement a simple HTTP server using the Node HTTP
core module
◆ Create and implement a web server to serve static HTML files from
a folder

03/06/2025 2
Networking Essentials
Client and Server
◆ Web applications are not stand-alone
◆ Many of them have a “Cloud” backend

03/06/2025 4
Client-Server Communication
◆ Network operations cause unexpected delays
◆ You need to write applications recognizing the asynchronous
nature of communication
 Data is not instantaneously available

03/06/2025 5
Hypertext Transfer Protocol (HTTP)
◆ A client-server ◆ HTTP Verbs – HEAD
communications protocol  GET
◆ Allows retrieving inter-  POST
linked text documents  PUT
(hypertext)  DELETE
 World Wide Web.  TRACE
 OPTIONS – CONNECT

03/06/2025 6
Hypertext Transfer Protocol (HTTP)

03/06/2025 7
HTTP Request Message

03/06/2025 8
HTTP Response Message

03/06/2025 9
HTTP Response Codes (Main ones)
◆ HTTP Response
 Server may send back data in a specific
format:
• eXtensible Markup Language (XML) –
Javascript Object Notation (JSON)

03/06/2025 10
Javascript Object Notation (JSON)
◆ http://www.json.org Example
{ "promotions": [
◆ Lightweight data interchange format {
"id": 0,
◆ Language independent * "name": "Weekend Grand Buffet",
"image": "images/buffet.png",
◆ Self-describing and easy to understand "label": "New",
"price": "19.99",
◆ Data structured as:
"description": "Featuring
 A collection of mouthwatering combinations . . . "
 name/value pairs: Ordered list of values }]

}
03/06/2025 11
Node HTTP module
Node HTTP module
◆ Core networking module supporting a high-performance foundation
for a HTTP stack
◆ Using the module:
const http = require('http');
◆ Creating a server:
const server = http.createServer(function(req, res){ . . . });
◆ Starting the server: server.listen(port, . . . );

03/06/2025 13
Node HTTP module
◆ Incoming request message information available through the first
parameter “req”
 req.headers, req.body,...
◆ Response message is constructed on the second parameter “res”
 res.setHeader(“Content-Type”,“text/html”);
 res.statusCode=200;
 res.write(‘HelloWorld!’);
 res.end(‘<html><body><h1>HelloWorld</h1></body></html>’);

03/06/2025 14
Node path Module
◆ Using path Module:
const path = require(‘path’);
◆ Some example path methods:
path.resolve(‘./public’+fileUrl);
path.extname(filePath);

03/06/2025 15
Node fs Module
◆ Use fs module in your application
const fs = require(‘fs’);
◆ Some example fs methods:
fs.exists(filePath, function(exists) { . . . } );
fs.createReadStream(filePath).pipe(res);

03/06/2025 16
Implement a simple HTTP
Server
A Simple HTTP Server
◆ Create a folder named node-http in the NodeJS folder and move
into the folder.
◆ In the node-http folder, create a subfolder named public.
◆ At the prompt, type the following to initialize a package.json file in
the node-http folder:

npm init

03/06/2025 18
A Simple HTTP Server
◆ Accept the standard defaults suggested until you end up with a
package.json file containing the following:

03/06/2025 19
A Simple HTTP Server
◆ Create a file named index.js and add the following code to it:

03/06/2025 20
A Simple HTTP Server
◆ Start the server by typing the
following at the prompt:
npm start

◆ Then you can type


http://localhost:3000 in your
browser address bar and see
the result.

03/06/2025 21
Using Postman to test HTTP Server

03/06/2025 22
Implement a server that returns
html files from a folder
Serving HTML Files
◆ In the public folder, create a
file named index.html and
add the following code to it:

◆ Similarly create an
aboutus.html file and add
the following code to it:

03/06/2025 24
Serving HTML Files

◆ Then update index.js as


follows:

03/06/2025 25
Start the HTTP Server

03/06/2025 26
Summary
◆ Understand the Networking Essentials
◆ Step by step to implement a simple HTTP Server
◆ Step by step to implement a server that returns html files from a
folder

03/06/2025 27

You might also like