0% found this document useful (0 votes)
24 views33 pages

Session 01-02-03-Introduction To Server-Side Development Evironment

This document provides an introduction to server-side development using Node.js. It outlines objectives like understanding Node.js basics, installing Node.js and NPM, writing simple Node applications and modules, and using callbacks and error handling. Key points covered include what Node.js is, its non-blocking I/O model, using NPM, creating a simple Node app, writing and requiring custom modules, and handling asynchronous operations through callbacks and errors.

Uploaded by

Trung Nguyễn
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)
24 views33 pages

Session 01-02-03-Introduction To Server-Side Development Evironment

This document provides an introduction to server-side development using Node.js. It outlines objectives like understanding Node.js basics, installing Node.js and NPM, writing simple Node applications and modules, and using callbacks and error handling. Key points covered include what Node.js is, its non-blocking I/O model, using NPM, creating a simple Node app, writing and requiring custom modules, and handling asynchronous operations through callbacks and errors.

Uploaded by

Trung Nguyễn
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/ 33

Introduction to Server-side

Development
Objectives
◆ Understand the basics of Node.js
◆ Download and install Node.js and NPM
◆ Verify that the installation was successful and your machine is ready for
using Node.js and NPM
◆ Write a simple Node application in JavaScript
◆ Understand the basics of Node modules and write simple file-based Node
modules
◆ Callback and Error handling in Node applications

07/26/2023 2
Node.js and NPM
What is Node.js?
◆ Node.js is an open source server environment
◆ Node.js is free
◆ Node.js runs on various platforms (Windows, Linux, Unix,
Mac OS X, etc.)
◆ Node.js uses JavaScript on the server
◆ JavaScript runtime built on Chrome V8 JavaScript Engine

07/26/2023 4
Why Node.js?

07/26/2023 5
Why is Node.js
The traditional web server handles a file
Node.js handles a file request
request
1. Sends the task to the computer's file system. 1. Sends the task to the computer's file system.
2. Waits while the file system opens and reads 2. Ready to handle the next request.
the file. 3. When the file system has opened and read
3. Returns the content to the client. the file, the server returns the content to the
4. Ready to handle the next request. client.

◆ Node.js eliminates the waiting, and simply continues with the next request.
◆ Node.js runs single-threaded, non-blocking, asynchronous programming,
which is very memory efficient.

07/26/2023 6
Why is Node.js

07/26/2023 7
What can Node.js do?
• Node.js can generate dynamic page content
• Node.js can create, open, read, write, delete, and close
files on the server
• Node.js can collect form data
• Node.js can add, delete, modify data in your database

07/26/2023 8
What is a Node.js File?
◆ Node.js files contain tasks that will be executed on certain events
◆ A typical event is someone trying to access a port on the server
◆ Node.js files must be initiated on the server before having any
effect
◆ Node.js files have extension “.js”

07/26/2023 9
Node Architecture

07/26/2023 10
Node.js Use Cases
◆ Utilities written in JavaScript for web development:
 Bower, Grunt, Gulp, Yeoman etc.
◆ Server-side Development
 Web server, Business logic, Database access

07/26/2023 11
Node Package Manager (NPM)
◆ Node package manager (NPM): manages ecosystem of node
modules / packages
◆ A package contains:
 JS files
 package.json (manifest)

07/26/2023 12
package.json
◆ A package.json file affords you a lot of great things:
 It serves as documentation for what packages your project depends on.
 It allows you to specify the versions of a package that your project can use
using semantic versioning rules.
 Makes your build reproducible, which means that its way easier to share
with other developers.
 Source: https://docs.npmjs.com/getting-started/using-a-package.json

07/26/2023 13
Initializing package.json
◆ To initialize a package.json file for your project, type at the prompt
in your project directory:
npm init
 Follow along and answer the prompts to initialize

07/26/2023 14
Setting up Node.js and NPM

07/26/2023 15
Installing Node
◆ To install Node on your machine, go to https://nodejs.org and click on the
Download button. Depending on your computer's platform (Windows,
MacOS or Linux), the appropriate installation package is downloaded

07/26/2023 16
Verifying the Node Installation
◆ Open a terminal window on your machine. If you are using a Windows
machine, open a cmd window or PowerShell window with admin privileges.
◆ To ensure that your NodeJS setup is working correctly, type the following at
the command prompt to check for the version of Node and NPM

node -v
npm -v

07/26/2023 17
Write a simple Node
application in JavaScript

07/26/2023 18
Write a simple Node application in JavaScript
◆ Go to a convenient location on your computer and create a folder
named NodeJS. Then move to this folder.
◆ Now create a folder named node-examples and then move into
this folder.
◆ At the prompt, type the following to initialize a package.json file in
the node-examples folder:
npm init

07/26/2023 19
Update the package.json file
◆ Accept the standard defaults suggested and then update the package.json file until you end
up with the file containing the following:
{
"name": "node-examples",
"version": "1.0.0",
"description": "Simple Node Examples",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": ”FPTU",
"license": "ISC"
}

07/26/2023 20
Create a file named index.js

07/26/2023 21
Run Node application
◆ To run the Node application, type the following at the prompt:
npm start

07/26/2023 22
Write a simple Node module

07/26/2023 23
Create a file named rectangle.js
◆ Now, create a file named rectangle.js, and add the following code to it:

◆ Run the Node application like before and observe that the result will be the same.

07/26/2023 24
Using Callback and Error
handling in Node applications

07/26/2023 25
Two Salient Features of JavaScript
◆ First-class functions: A function can be treated the same way as
any other variable
◆ Closures:
 A function defined inside another function has access to all the variables
declared in the outer function (outer scope)
 He inner function will continue to have access to the variables from the outer
scope even after the outer function has returned.

07/26/2023 26
Asynchronous Programming

07/26/2023 27
Node, Async I/O and Callbacks

07/26/2023 28
Event Loop timers: this phase executes callbacks scheduled

by setTimeout() and setInterval().


• I/O callbacks: executes almost all callbacks with the
exception of close callbacks, the ones scheduled by
timers, and setImmediate().
• idle, prepare: only used internally.
• poll: retrieve new I/O events; node
• will block here when appropriate.
• check: setImmediate() callbacks are
• invoked here.
• close callbacks:
• e.g. socket.on('close', ...).
07/26/2023 29
Update rectangle.js file:

07/26/2023 30
Update index.js file:

07/26/2023 31
Run the Node application:

07/26/2023 32
Summary
◆ Understand the basics of Node.js
◆ Understand the download and install Node.js and NPM
◆ Write and run a simple Node application in JavaScript
◆ Understand the basics write simple file-based Node modules
◆ Understand the using Callbacks and Error handling in Node
applications

07/26/2023 33

You might also like