Node and the HTTP Module
Objectives and Outcomes
In this exercise, you will explore three core Node modules: HTTP, fs and path. At the end of this
exercise, you will be able to:
Implement a simple HTTP Server
Implement a server that returns html files from a folder
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 [Link] file in the node-examples folder:
npm init
Accept the standard defaults suggested until you end up with a [Link] file containing the
following:
{
"name": "node-http",
"version": "1.0.0",
"description": "Node HTTP Module Example",
"main": "[Link]",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": "FPTU",
"license": "ISC"
}
Create a file named [Link] and add the following code to it:
const http = require('http');
const hostname = 'localhost';
const port = 5000;
const server = [Link]((req, res) => {
[Link]([Link]);
[Link] = 200;
[Link]('Content-Type', 'text/html');
[Link]('<html><body><h1>Hello, World!</h1></body></html>');
})
[Link](port, hostname, () => {
[Link](`Server running at [Link]
});
1
Start the server by typing the following at the prompt:
npm start
Then you can type [Link] in your browser address bar and see the result.
You can also use postman chrome extension to send requests to the server and see the
response. Alternately, you can download the stand-alone Postman tool from
[Link] and install it on your computer.
Initialize a Git repository, check in the files and do a Git commit with the message "Node HTTP
Example 1".
Serving HTML Files
In the public folder, create a file named [Link] and add the following code to it:
<html>
<title>This is [Link]</title>
<body>
<h1>[Link]</h1>
<p>This is the contents of this file</p>
</body>
</html>
Similarly create an [Link] file and add the following code to it:
<html>
<title>This is [Link]</title>
<body>
<h1>[Link]</h1>
<p>This is the contents of the [Link] file</p>
</body>
</html>
Then update [Link] as follows:
...
const fs = require('fs');
const path = require('path');
...
const server = [Link]((req, res) => {
[Link]('Request for ' + [Link] + ' by method ' + [Link]);
if ([Link] == 'GET') {
o var fileUrl;
o if ([Link] == '/') fileUrl = '/[Link]';
o else fileUrl = [Link];
o var filePath = [Link]('./public'+fileUrl);
2
o const fileExt = [Link](filePath);
o if (fileExt == '.html') {
o [Link](filePath, (exists) => {
if (!exists) {
[Link] = 404;
[Link]('Content-Type', 'text/html');
[Link]('<html><body><h1>Error 404: ' + fileUrl +
o ' not found</h1></body></html>');
return;
}
[Link] = 200;
[Link]('Content-Type', 'text/html');
[Link](filePath).pipe(res);
o });
o }
o else {
o [Link] = 404;
o [Link]('Content-Type', 'text/html');
o [Link]('<html><body><h1>Error 404: ' + fileUrl +
' not a HTML file</h1></body></html>');
o }
}
else {
o [Link] = 404;
o [Link]('Content-Type', 'text/html');
o [Link]('<html><body><h1>Error 404: ' + [Link] +
' not supported</h1></body></html>');
}
})
...
Start the server and send various requests to it and see the corresponding responses.
Do a Git commit with the message "Node HTTP Example 2".
Conclusions
In this exercise you learnt about using the Node HTTP module to implement a HTTP server.