0% found this document useful (0 votes)
7 views51 pages

W2 - Lecture Notes

The document provides an overview of Node.js, highlighting its environment for running JavaScript outside the browser, its built-in modules, and the differences between browser and Node.js applications. It also covers the installation and usage of Express.js, including routing, middleware, and package management with npm. Key concepts such as modules, file handling, and error-handling middleware are discussed, along with practical examples and commands.

Uploaded by

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

W2 - Lecture Notes

The document provides an overview of Node.js, highlighting its environment for running JavaScript outside the browser, its built-in modules, and the differences between browser and Node.js applications. It also covers the installation and usage of Express.js, including routing, middleware, and package management with npm. Key concepts such as modules, file handling, and error-handling middleware are discussed, along with practical examples and commands.

Uploaded by

95hfdp6grt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Computer Engineering

NodeJS

Rakhymbayeva Nazerke Imanbakhytovna


[Link]@[Link]

2024
NodeJS

● Environment to run JS outside Browser


● Built on Chrome’s V8 JS Engine
● Big Community
● Full-Stack

2
Difference between Browser and [Link]

Browser [Link]

● DOM ● No DOM
● Window ● No Window
● Interactive Apps ● Server Side Apps
● No Filesystem ● Filesystem
● Fragmentation ● Versions
● ES6 Modules ● CommonJS

3
4
5
Typing using cmd

To exit:

● CTRL+C
● CTRL+D
● .exit

6
7
8
sayHi(‘name’)

sayHi(nazerke)

9
CommonJS, every file is module (by default)

Modules - Encapsulated Code (only share minimum)

10
Files that you will need
[Link] 6-alternative-fl[Link]

7-mind [Link]
[Link]

11
Built-in Modules

OS

PATH

FS

HTTP

12
13
OS module

14
PATH module

15
FS module (sync)

flag : a - append

Otherwise it will
override

16
FS module (async)

17
HTTP module

18
HTTP module

19
NPM Package

20
NPM ([Link])

21
NPM - global command, comes with node
NPM —version

Local dependency - use it only this particular Global dependency - use it any project
project

npm i <packageName>
npm install -g <packageName>

sudo install -g <packageName> (mac)

22
[Link]

Manifest file (stores important info about


project/package)

It is crucial when we share the project with


other developers

1. Manual approach (create [Link] in the


root, create properties etc.)
2. npm init (step by step, press enter to skip)
3. npm init -y (everything default)

23
This is where all the dependencies are stored

npm creates it

all dependencies automatically saved into the folder

While uploading the project to the github, we don’t


need to push all the folder of node_modules just use
[Link] to install all dependencies using npm
install.

24
nodemon
npm i nodemon -D

nodemon is a tool that helps develop [Link]


based applications by automatically
restarting the node application when file
changes in the directory are detected.

nodemon does not require any additional


changes to your code or method of
development.

nodemon is a replacement wrapper for node.


To use nodemon, replace the word node on
the command line when executing your
script.

25
Further reading

Understanding of dependencies, development dependencies, peerDependencies

26
npm start

Stop: CTRL+C

27
Further reading about [Link]

28
[Link] Tutorial

29
Introduction

[Link] is a minimal and flexible [Link] web application framework that provides a list of features for
building web and mobile applications easily.

● Built on [Link] for fast and scalable server-side development.


● Simplifies routing and middleware handling for web applications.
● Supports building REST APIs, real-time applications, and single-page applications.
● Provides a lightweight structure for flexible and efficient server-side development.

30
31
Installing Express

STEP-1: Creating a directory for our project and make that our working directory.

STEP-2: Using npm init command to create a [Link] file for our project.
(Keep pressing enter and enter “yes/no” accordingly at the terminus line.)

STEP-3: Installing Express (in gfg(name of your folder) folder):

STEP-4: Verify that [Link] was installed on your Windows:

32
Routing

Routing refers to how an application’s endpoints (URIs) respond to client requests.

33
Route methods

a special routing method, [Link](), used to load


middleware functions at a path for all HTTP request
methods.

34
Route paths

Route paths can be strings, string patterns, or regular expressions.

the path string for requests at “/data/$book”, would be “/data/([\$])book”

Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all
the possibilities in defining route paths.

Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern
matching.

Query strings are not part of the route path.

35
Examples

36
Route parameters

Route parameters are named URL segments that are used to capture the values specified at their position
in the URL.

To define routes with route parameters,

simply specify the route parameters

in the path of the route.


37
Route parameters

The name of route parameters must be made up of Appending a regular expression in parentheses (())
“word characters” ([A-Za-z0-9_]).

38
Route handlers

Route handlers can be in


the form of a function, an
array of functions, or
combinations of both

Single callback function

More than one callback function can handle a


route

39
Arrays of callback functions Combination of independent functions and arrays
of functions

40
Response methods

41
Using middleware

An Express application is essentially a series of middleware function calls.

Middleware functions are functions that have access to the request object (req), the response object
(res), and the next middleware function in the application’s request-response cycle. The next middleware
function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

● Execute any code.


● Make changes to the request and the response objects.
● End the request-response cycle.
● Call the next middleware function in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass
control to the next middleware function. Otherwise, the request will be left hanging. 42
Types of middleware

Application-level middleware

Router-level middleware

Error-handling middleware

Built-in middleware

Third-party middleware

43
Application-level middleware

a middleware function with no mount path a middleware function mounted on the /user/:id
path

44
Application-level middleware

a route and its handler function GET (middleware


system)

a middleware sub-stack

45
Router-level middleware

a middleware sub-stack shows request info for


a middleware function with no mount path any type of HTTP request to the /user/:id path

46
47
Error-handling middleware

Define error-handling middleware functions in the same way as other middleware functions, except with
four arguments instead of three, specifically with the signature (err, req, res, next):

48
Built-in middleware

● [Link] serves static assets such as HTML files, images, and so on.
● [Link] parses incoming requests with JSON payloads. NOTE: Available with Express 4.16.0+
● [Link] parses incoming requests with URL-encoded payloads. NOTE: Available with
Express 4.16.0+

49
Third-party middleware

50
Thank you for your attention

51

You might also like