Welcome To The World of Node - JS: by Ojas Joshi
Welcome To The World of Node - JS: by Ojas Joshi
Welcome To The World of Node - JS: by Ojas Joshi
World of Node.js
Welcome to the world of Node.js. You're about to embark on a
journey into server-side JavaScript, learning how to build powerful
web applications. This presentation will guide you through the basics,
from installation to building your first app.
by Ojas Joshi
What is Node.js?
Node.js is a JavaScript runtime environment that allows you to run JavaScript on the server side. Unlike traditional server-side languages,
Node.js employs an asynchronous, event-driven architecture, making it efficient for handling numerous requests simultaneously.
Node.js excels at building real-time Node.js is highly scalable, meaning it Using JavaScript for both front-end and
applications like chat apps, online can handle increasing traffic and user back-end simplifies development and
games, and live dashboards, where loads without compromising promotes code reuse. Node.js has a
data needs to be updated instantly. performance. This is essential for web vast ecosystem of libraries and tools
applications with a growing user base. available through npm.
• Live chat
• Video conferencing • Streaming services • Consistent development
• Social media platforms environment
• Online gaming
• E-commerce sites • Rich set of libraries and frameworks
• Strong community support
Companies Using Node.js
Company Application
node -v
This will display the installed Node.js version.
npm -v
This will display the installed npm version.
Understanding Node.js Modules
Node.js modules are like building blocks that allow you to organize and reuse code. There are two types of modules: core
modules (built-in) and custom modules (created by you).
4 path 5 events
For manipulating file paths and ensuring For handling events, allowing you to trigger actions
consistency across different operating systems. when specific events occur.
Creating Custom Modules
Custom modules allow you to organize your code into reusable components, making your projects more maintainable and
scalable. They enable you to create specialized functions that are specific to your application.
Define Functions
Define the functions you want to export from the module.
Export Functions
Use module.exports to export the functions so they can be used in other files.
1 npm init
Initializes your Node.js project by creating a package.json file, which lists all the
packages you need.
2 npm install
Installs packages from the npm registry. You can specify the package name and
version, e.g., npm install express@4.17.1.
3 npm update
Updates installed packages to their latest versions.
4 npm uninstall
Removes installed packages from your project. You can specify the package name,
e.g., npm uninstall express.
Popular npm Packages
npm has a vast collection of packages that you can use to extend the functionality of your Node.js applications. These packages offer pre-built
solutions for various tasks, saving you time and effort.
Express.js Mongoose
A popular web framework for building robust and scalable web A MongoDB ODM (Object Document Mapper) that simplifies
applications. interaction with MongoDB databases.
Next Steps: Building Your First
Node.js App
Now that you have a basic understanding of Node.js, its modules, and npm, you're ready to start
building your first Node.js application. The next step is to choose a project idea and explore the
vast possibilities offered by this powerful technology.
REST API
Build a RESTful API to interact with data sources like databases or external services.
2 npm install
Installs packages from npm.
3 npm update
Updates installed packages to their latest versions.
4 npm uninstall
Removes packages from your project.
Creating a Web Server with
Node.js
1 Setting Up the Project
Create a folder for your project and a server.js file.
Send Response
Send an HTML message back to the client.
File System Module in Node.js
fs Module
Provides methods to interact with the file system.
Asynchronous Operations
Allows other tasks to run while file operations are in progress.
fs.readFile
Reads the content of a file asynchronously.
fs.writeFile
Writes content to a file asynchronously.
Reading Files Asynchronously
fs.readFile Callback Function Example
Reads the content of a file Handles the result of the file read fs.readFile('example.txt', 'utf8', (err,
asynchronously. operation. data) => { ... });
Routing
Handles different URL requests.
Middleware
Handles request data before sending a response.
Scalability
Makes it easier to build large-scale apps.
Setting Up Express
1 Create Project Folder
Create a folder for your project and a package.json
file.
2 Install Express
Run npm install express to install Express.js.
Before you reach your destination (route), you pass through multiple
checkpoints that check your ID (authorization), scan you for metal
(data validation), and sometimes direct you to the right path (routing).
Error Handling in Express.js
1 Analogy: Customer Service
Imagine a customer service desk where something goes wrong. The situation
can be handled by solving the problem or forwarding it to a higher level.
4 Error-handling Middleware
This middleware is used to catch errors. By convention, it has four arguments,
with the first being the error object (err).
Middleware Flow Example (Bringing it Together)
Request Logging
1
Logs every request to the console.
Authentication Check
2
Verifies user authentication.
Dashboard Route
3
Accessible only to authenticated users.
Error Handling
4
Catches and handles errors.
Middleware functions run in order, processing the request before it reaches the route handler. Errors are caught by the error-handling middleware
at the end.
What are Static Resources?
Static Resources Dynamic Content
Static resources are parts of a website that don’t change Dynamic content is the opposite of static resources.
depending on the user or time.
It changes based on user actions or time, such as movie
Examples include images, stylesheets, or scripts. listings or news articles.
Serving Static Resources
Mongoose ODM
We'll use Mongoose, an Object Document Mapper (ODM)
library, to interact with MongoDB in a structured way.
Learning Objectives
• Install and set up MongoDB locally
2 Middleware Execution
Middleware functions are executed in the order they are defined.
3 Route Handler
The request reaches the appropriate route handler.
4 Response Sent
The server sends a response back to the client.
Serving Static Resources
Static Files
Express.js can serve static files like HTML, CSS, and JavaScript from a designated
directory.
Public Directory
By default, Express looks for static files in a 'public' directory within your project.
Serving Files
Use the `express.static()` middleware to configure the directory for serving static files.
Setting Up Node.js with MongoDB
While running MongoDB locally is useful for development, you often need a scalable, cloud-based solution for production. This is where MongoDB Atlas comes in.
MongoDB Atlas is a cloud-based database service that makes it easy to deploy and manage MongoDB databases. You don’t have to worry about server maintenance, backups, or
scaling.
Create an Account
1 Visit MongoDB Atlas and create a free account.
Create a Cluster
2
Follow the setup instructions to create a new cluster.
Whitelist Your IP
3
MongoDB Atlas requires you to whitelist the IP addresses from which you’ll connect.
MongoDB Atlas
We've also explored how to use MongoDB Atlas for a cloud-based
database.
Mongoose Models
We've learned how to create and retrieve documents (users) using
Mongoose models.