Lab03 - Introduction to NodeJS
Lab03 - Introduction to NodeJS
js
UNIVERSITY OF INFORMATION TECHNOLOGY
1
3. INTRODUCTION TO NODE.JS
Website Design and Development
A. OVERVIEW
1. Learning Objective
In this lab, students will be:
▪ We will work with the npm (Node Package Manager) to manage the third
libraries and external packages for web applications.
Download and install the suitable NodeJS version from https://nodejs.org/. Follow
the installation instructions for your operating system. You are recommended to
2
choose the LTS (Long-Term Support) to have support with updates, bug fixes,
and security patches for a more extended period.
Figure 2: Open the installer and follow the instructions to install Node.js
The Visual Studio Code is recommended for developing the Node.js application.
It's a free coding editor that helps you start coding quickly. You need to browse to
https://code.visualstudio.com/ and choose the appropriate installer for your
machine's platform to download and install.
3
Figure 3: Visual Code Studio
3. Background knowledge
a. Primitive Types
▪ String
▪ Number
▪ Boolean
▪ Undefined
▪ Null
▪ RegExp
Everything else is an object in Node.js.
b. Function
function Display(x) {
console.log(x);
}
// Display(100);
4
c. Process object
Each Node.js script runs in a process. It includes a process object to get all the
information about the current process of the Node.js application.
> process.execPath
'C:\\Program Files\\nodejs\\node.exe'
> process.pid
2156
> process.cwd()
'D:\\App\\'
d. Node.js module
Each module in Node.js has its context, so it cannot interfere with other modules
or pollute the global scope. Also, each module can be placed in a separate .js file
under a separate folder.
Node.js core modules are compiled into Node.js binary distribution and load
automatically when the Node.js process starts. However, it would be best to
import the core module to use in your application.
Module Description
http Includes classes, methods, and events to create Node.js http
server.
url Includes methods for URL resolution and parsing.
querystring Includes methods to deal with query string.
path Includes methods to deal with file paths.
fs Includes classes, methods, and events to work with file I/O.
util Includes utility functions useful for programmers.
To use Node.js core or NPM modules, you first need to import it:
//e.g.,
// var http = require('http')
5
The required function will return an object, function, property, or any other
JavaScript type, depending on what the specified module returns.
Node Package Manager (NPM) is a command line tool that installs, updates, or
removes Node.js packages in your application. It is also an online repository for
open-source Node.js packages.
NPM is included by default when installing Node.js. You can check the running
version of NPM by using the npm -v command in the terminal. If you have an
older version of NPM, then you can update it to the latest version with the
command npm install npm -g
// Install package
npm install <package name>
// Update package
npm update <package name>
// Remove package
npm uninstall <package name>
6
B. LAB TASKS
You should do the following tasks in sequence. If you already know about Node.js
development, you can skip the first four tasks and complete the final task for this
lab.
7
Figure 4: The first web application
2. Routing
Routing refers to determining how an application responds to a client request to
a particular endpoint, a URI (or path), and a specific HTTP request method (GET,
POST, and so on).
Each route can have one or more handler functions executed when the route is
matched.
app.METHOD(PATH, HANDLER)
Where:
8
});
module.exports = router;
3. Template engines
A template engine enables you to use static template files in your application. At
runtime, the template engine replaces variables in a template file with actual
values, transforms the template into an HTML file, and then sent to the client.
This approach makes it easier to design an HTML page.
Some popular template engines that work with Express are Pug, Mustache, and
EJS. The Express application generator uses Jade as its default but supports several
others. The example in task 1 has chosen EJS as a template engine.
The template files should be placed in the views folder. With express-generator,
views/index.ejs file was created by default. Look at this file and practice on it.
routes/index.js
1 /* GET home page. */
2 router.get('/', function(req, res, next) {
3 res.render('index', { title: 'Express' });
4 });
5
The function res.render() will render the content from the template:
▪ First argument: the name of the template file (without file extension)
views/index.ejs
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title><%= title %></title>
5 <link rel='stylesheet' href='/stylesheets/style.css' />
9
6 </head>
7 <body>
8 <h1><%= title %></h1>
9 <p>Welcome to <%= title %></p>
10 </body>
11 </html>
12
4. Form handling
Forms can be submitted using the GET method or the POST method.
GET method forms are recommended for simple forms like a search query or
specifying a user's id. The form submission details are passed in the request URL
and can be seen in the browser address bar.
POST method forms are recommended for forms with sensitive data, such as user
login, and forms that send data to the backend, such as submitting an article. The
POST method is the only way to upload files.
Below are two ways of sending data to Express using the GET method. Both these
methods work right out of the box, and you don't need to install any packages to
make them work.
▪ GET parameters: the standard for GET form submissions across all browsers
and backend systems. It consists of a key and a value in the URL string,
specified after a ?. More than a key-value pair can be defined by using & as
a demarcator.
10
5. Greeting application
11
C. REQUIREMENTS AND SUBMISSION
You must complete all tasks in section B (Lab tasks). Advanced tasks are optional,
and you could get bonus points for completing those tasks. This lab is designed
for individuals. You have seven days to clear all tasks.
▪ If the submission contains more than one file, you must place all
files/folders inside a folder whose name pattern is "StudentID-Name.”
Then, archive this folder in the zip format.
Your submissions must be your own. You are free to discuss with other classmates
to find the solution. However, copying is prohibited, even if only a part of your
report. Both reports of the owner and the copier will be rejected. Please remember
to cite any source of the material (website, book, etc.) that influences your solution.
D. REFERENCES
1. W3Scholl HTML Tutorial: https://www.w3schools.com/html/
2. HTML elements reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element
3. HTML Tutorial: https://www.geeksforgeeks.org/html
4. CSS: https://www.w3schools.com/css/default.asp