Professional Documents
Culture Documents
Node Fundamentals
Node 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
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
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
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
npm search <pkg> Searches for packages in the npm registry npm search express
npm run <script> Runs an npm script defined in package.json npm run start
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.
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