In this module, we will discuss about the background theory behind the interaction between a web server and a web client. We will begin with the terminologies regarding the concepts about the interaction or communication between the web client and the web server. The knowledge will pave the road toward the required programming tasks when we develop a web server.
We will cover the fundamental JavaScript Programming Language features, and Node.JS with the HTTP package. Our goal is to develop a Web Server to be running as the back-end of an application.
Web accessing is known as a service and HTTP is a protocol. A protocol is a set of rules both the client and the server comply to achieve successful communications. An HTTP protocol is used by a Web Client to communicate with a Web Server.
Application layer protocols and applications are two different concepts. For example, file transfer is an application that provides the capability of copying files from one machine to another. FTP is a protocol (RFC 959). When web accessing is an application, the term HTTP represents the protocol used by the client and the server to achieve web page accessing & displaying. In the first part of this semester, we will explore some applications and the protocols associated with them.
The Internet started out as a research network mainly used by researchers, university students, and national labs to log in remotely, to transfer files, and to receive news, e- mail messages. Around 1992, WWW began to draw the attention of almost every discipline. It is simply based on one simple protocol, called Hyper-Text Transfer Protocol or HTTP as illustrated in Figure 1.
HTTP is essentially a protocol used for representing message formats between a Web Browser and a Web Server to communicate with each other. The client may send a request of a web page to the server, and the server retrieves the web page and sends the web page back to the client. The Web Browser locating at the client machine displays the web page.
It is very simple. But it provides the platform for the future web applications. The client and the server usually are executed on different hosts. When a Uniform Resource Locator (URL) is passed onto the Web Browser, the Web Browser will construct an HTTP request, sends the request message to the server via the Internet. The server serves one request at a time without checking the current state of the client.
You should complete the hands-on introduction to Javascript on Linked Learning (accessible via WCUPA)
wait until this is done) is inefficient.event loop, which enable non-blocking operations in JavaScript.call stack - LIFO (Last In/First Out)libuv library (Node.js)callback queue (macrotasks) or job queue (microtasks), depending on their priority.setTimeout, setInterval)Promises (.then(), .catch(), .finally(), and queueMicrotask()) calls. Read the following article: Async JavaScript: From Callbacks, to Promises, to Async/Await
1-javascript inside the /apps directory.test.js inside the 1-javascript directory with the following content:
1
2
3
a = 10;
b = 20;
console.log(a + b);
1-javascript and run:
1
2
cd 1-javascript
node test.js
30 to be printed to the screen.+, -, ++, --, *, /, % +=, -=, *=, /=, %= ==, ===, !=, &&, || event_loop.js inside the 1-javascript directory with the following content:
1
2
3
4
5
6
7
8
9
10
11
console.log('Start');
setTimeout(() => {
console.log('Timeout callback'); // callback queue - macrotasks
}, 0);
Promise.resolve().then(() => {
console.log('Promise callback'); // job queue - microtasks
});
console.log('End');
1-javascript and run:
1
2
cd 1-javascript
node event_loop.js
1
2
3
4
5
6
7
8
9
10
11
12
We just learned about JavaScript and use JavaScript as a server-side technology. Now,
we can begin to look into the node.js platform. We will begin our journey about
node.js using the HTTP library module in the following lessons from Unit 1 of the
textbook.
- Lesson 3 – Creating a Node.js module
- Lesson 4 – Building a simple web server in Node.js
- Lesson 5 – Handling incoming data
- Lesson 6 – Writing better routes and serving external files
- Lesson 7 – Capstone: Creating your first web application
The listings mentioned in the lessons will be from the textbook.
require is a Node.js global object used to locally introduce methods and objects from other modules. If the module is a user-defined module, e.g., ./messages, you don’t have to prepare for it; you may use npm install followed by npm run start.express, to be included in this application with main.js statement require("express") in main.js, you will need to enter the command npm install express before you can enter node main.js, npm run start, or npm start to bring up the web server main.js. This is true for all server programs hereinafter.http or require http-status-codes”. You will need to enter “npm install http http-status-codes before you can enter node main.js.node_modules will have a lot of contents. It can be regenerated. The package-lock.js is also a re-generable file.node printMessages.js main.js.
1
2
3
npm init --yes
npm install --save
npm install cities –-save
main.js as in Listing 3.4node main.js.Later, we will begin to rely on reading the textbook more and we only include the explanations of the code that requires attentions in the lecture notes. It is highly recommend that you read all lessons in
Unit_0,Unit_1,Unit_2,Unit_3,Unit_4, and the first two chapters inUnit_6by the end of this course. You may skipUnit_5.
http and http-status-code before executing the web server.
1
2
3
npm init --yes
npm install http http-status-codes
node main.js
In previous lab experiment, the web browser displays Hello Universe! after a URL http://127.0.0.1:3000 is entered. If you try http://127.0.0.1:3000/testing, it still displays the same. The reason is because the server does nothing but displaying the string Hello Universe.
In this lesson, we will begin to learn how to complete some processing before rendering a web page. We use the term render to refer to the activities of formatting a web page into an HTTP reply message and sending it back to the browser.
npm install http http-status-codes.node main.js andhttp://127.0.0.1:3000.taps behind the string localhost:3000. node main1.js
1
curl --data "username=Jon&password=secret" http://127.0.0.1:3000
In this lesson, we need to add some routes after localhost:3000. For example, we need to enter localhost:3000/contact or localhost:3000/about, etc. We need to modify the main.js and add some “route handling” code.
Use the following views/index.html file (Listing 6.1) in the views folder for all three labs.
main.js from Listing 6.2.node main.js in the terminallocalhost:3000/about or localhost:3000/contact main.js from a combination of Listing 6.2 andListing 6.3 (see textbook instructions).localhost:3000/about or localhost:3000/contact main.js from Listing 6.6router.js from Listing 6.5node main.js in the terminallocalhost:3000/about or localhost:3000/contact Carrying out the following steps to create your first web application project:
1
2
npm init --yes
npm install --save
main.js as Listing 7.3contentTypes.js as Listing 7.4utils.js as Listing 7.5router.js as Listing 7.6node main.js and navigate to http://127.0.0.1:3000 to see the application’s home page.This lesson concludes our discussion about using the node.js http module to develop a web server. It is instructive to know that the activities for registering a route in a web server include the following: