You are on page 1of 42

Node JS Fundamentals

Introduction

 Backend refers to the server-side of a software application that handles data processing, storage, and
communication.
 It works behind the scenes to ensure the functionality, security, and performance of the application
What really a software
backend is in terms of
what it can do?
Backend & Server Logic

Server Logic
 Backend houses the core logic and algorithms that process data, perform calculations, and execute
business logic /rules
Backend & Server Logic

Database Management
 It manages data storage, retrieval, and manipulation using databases such as MySQL, PostgreSQL, or
NoSQL solutions like MongoDB.
Backend & API Handling

API Handling
 Backend creates APIs (Application Programming Interfaces) to enable communication between the
frontend and backend, allowing data exchange and functionality access.
Backend & User Authentication

User Authentication
 It handles user authentication and authorization, ensuring secure access to the application's features and
resources.
Backend & Security

Security
 Backend implements security measures like data encryption, input validation, and access controls to
protect sensitive information.
Backend & Server Management

Server Management
 It manages server resources, load balancing, and scaling to ensure optimal performance and
responsiveness.
Backend & Application Logic

Application Logic
 Backend executes complex processes and workflows, orchestrating different components to deliver the
desired user experience.
Backend & Integration

Integration
 It integrates with external services, APIs, and third-party tools to extend the application's capabilities.
Backend & Notifications

Notifications
 Backend manages notifications, sending emails, push notifications, or other alerts to users based on
events or triggers.
Backend & Error Handling

Error Handling
 Backend handles errors and exceptions, logging issues for debugging and ensuring graceful degradation
of the application during unexpected scenarios
Description of NodeJs fundamentals

 Node.js is an open-source, server-side runtime environment that allows you to execute JavaScript code
outside of a web browser.
 It's built on the V8 JavaScript engine by Google and provides a platform for building scalable and efficient
network applications.
Students helpful resources
Usage of Node Package Manager (npm)

 Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js
packages in your application.
 It is also an online repository for open-source Node.js packages.
 The node community around the world creates useful modules and publishes them as packages in this
repository.
 NPM is included with Node.js installation.
 After you install Node.js, verify NPM installation by writing the following command in terminal or
command prompt.
Upgrade an older version of NPM

 If you have an older version of NPM then you can update it to the latest version using the following
command.
To access NPM help

 To access NPM help, write npm help in the command prompt or terminal window.
Two modes of operation of NPM

 NPM performs the operation in two modes: global and local.


 In the global mode, NPM performs operations which affect all the Node.js applications on the computer
whereas in the local mode, NPM performs operations for the particular local directory which affects an
application in that directory only
Install Package Locally

 Use the following command to install any third party module in your local Node.js project folder.
 For example, the following command will install ExpressJS into MyNodeProj folder.

 All the modules installed using NPM are installed under node_modules folder.
 The above command will create ExpressJS folder under node_modules folder in the root folder of your
project and install Express.js there.
Node modules

 A Node module is a self-contained file or directory of related code that can be included in your Node.js
application.
 Modules are used to organize code, make it reusable, and distribute it easily.
Types of modules

 Modules are of three types:


 Core Modules
 Local Modules
 Third-party Modules
Core Modules

 Node.js has many built-in modules that are part of the platform and come with Node.js installation.
 These modules can be loaded into the program by using the required function
Example

 In the above example, the require() function returns an object because the Http module returns its
functionality as an object.
 The function http.createServer() method will be executed when someone tries to access the computer on
port 3000.
 The res.writeHead() method is the status code where 200 means it is OK, while the second argument is an
object containing the response headers.
Exercises

List other important core modules in Node.js


Local Modules

 Unlike built-in and external modules, local modules are created locally in your Node.js application.
 Let’s create a simple calculator module that calculates various operations.
Local Modules: Create a calc.js file that has the following code
Third-party modules

 Third-party modules are modules that are available online using the Node Package Manager(NPM).
 These modules can be installed in the project folder or globally.
 Some of the popular third-party modules are Mongoose, express, angular, and React.
Quiz

 Create a nodejs module that can be used to perform statistical operations like mean, median or standard
deviation
Add Dependency into package.json

 Use --save at the end of the install command to add dependency entry into package.json of your application.
 For example, the following command will install ExpressJS in your application and also adds dependency
entry into the package.json. [npm install express --save]
Some other NPM commands

Command Description Example

npm install Installs dependencies defined in package.json npm install

npm install <pkg> Installs a package locally npm install lodash

npm install -g <pkg> Installs a package globally npm install -g nodemon

npm init Creates a package.json file npm init

npm update Updates packages to their latest versions npm update

npm uninstall <pkg> Uninstalls a package npm uninstall lodash

npm search <pkg> Searches for packages in the npm registry npm search express

npm list Lists installed packages npm list

npm outdated Checks for outdated packages.

npm run <script> Runs an npm script defined in package.json npm run start

npm publish Publishes a package to the npm registry npm publish

npm login/logout Logs in or out from npm registry.

npm cache clean Clears the npm cache npm cache clean
Incrementing semantic versions in published
packages
Home work: Publishing a custom package

1. Use notes
2. Read: 1.2.5. Publishing a custom package
3. Create a unique node package of your choice and publish it to the npm registry /10marks
4. Make sure your package is addressing a real-world programming challenges /10marks
5. Apply semantic versioning /10markss
6. Use it in your existing project /10m
Synchronous vs Asynchronous programming
models.

 Async is multi-thread, which means operations or programs can run in parallel.


 Sync is single-thread, so only one operation or program will run at a time.
 Async is non-blocking, which means it will send multiple requests to a server.
 Sync is blocking — it will only send the server one request at a time and will wait for that request to be
answered by the server.
 Async increases throughput because multiple operations can run at the same time.
 Sync is slower and more methodical.
Promises

 A Promise is a special JavaScript object.


 It produces a value after an asynchronous operation completes successfully, or an error if it does not
complete successfully due to time out, network error, and so on.
 They are used to handle asynchronous operations such as fetching data from a server, reading files, or any
other operations that may take some time to complete.
Promises have three states

1. Pending: Initial state, the operation hasn't completed yet.


2. Fulfilled (Resolved): The operation completed successfully.
3. Rejected: The operation failed.
To create a promise
resolve and reject

 resolve and reject. These functions are pre-defined by the JavaScript engine, so we don’t need to create
them.
 We should only call one of them when ready.
Promises and Chaining
Async/await

 The word “async” before a function means one simple thing: a function always returns a promise. Other
values are wrapped in a resolved promise automatically.
 async makes a function return a Promise
 await makes a function wait for a Promise
Async/await

 The function execution “pauses” at the line (*) and resumes when the promise settles, with result becoming
its result. So the code above shows “done!” in one second.
 Let’s emphasize: await literally suspends the function execution until the promise settles, and then
resumes it with the promise result. That doesn’t cost any CPU resources, because the JavaScript engine
can do other jobs in the meantime: execute other scripts, handle events, etc

You might also like