Mongodb, Express, and Nodejs Notes: What Is Node Js and Why We Use Node Instead of Other Technology ?
Mongodb, Express, and Nodejs Notes: What Is Node Js and Why We Use Node Instead of Other Technology ?
Express, and
Nodejs
NOTES
What is node js and why we use node
instead of other technology ?
Node.js is a javascript runtime built on google’s open-source v8
javascript engine.
We already use javascript before and but inside the browser because
any browser understand html, css and javascript no matter if its vanilla
js or react code , all gets executed right in the browser , then the
browser is then the javascript runtime BUT what if we could take
javascript out of the browser and simply execute javascript code
somewhere else without all the restrictions that we have in the browser
well we actually can with the help of node.js
// 1. Read file
const fs = require("fs");
// it takes two arguments the first one is the path to
the file that we are reading and then also the character
encoded
const textIn = fs.readFileSync("./txt/input.txt", "utf-
8");
console.log(textIn);
// 2. Write file
fs.writeFileSync("./txt/output.txt", textOut);
console.log("File written!");
Blocking and Non-
blocking:
Asynchronous Nature
of Node.js
·
Synchronous Code in Node.js:
Code executed line by line, waiting for each statement to complete before moving on.
Blocking nature can lead to inefficiencies, especially with slow operations.
· Asynchronous, Non-blocking Code in Node.js:
Solution to blocking code issues in Node.js.
Involves uploading heavy work to the background, allowing the rest of the code to
continue executing.
Utilizes callback functions to handle results once background tasks are complete.
· Single-Threaded Nature of Node.js:
Node.js processes operate on a single thread.
All users accessing an application share this thread.
Synchronous code execution can lead to delays for other users.
· Callback Hell:
Nested callbacks can make code hard to read and manage.
Triangular shape in code structure is a sign of callback hell.
· Use of Callbacks in Asynchronous Code:
Callbacks are essential for handling asynchronous operations effectively.
Callback functions are registered to process results once background tasks are finished.
· Callback Hell Escape:
More advanced tools like ES6 promises and ES8 async/await provide elegant solutions.
However, the course focuses on callbacks, aligning with Node.js's original design.
· Practical Implementation:
Demonstrated asynchronous behavior with the example of reading a file using the
readFile function.
Emphasized the importance of avoiding blocking code for a better user experience.
· Future Topics:
Promised a deeper understanding of how Node.js handles asynchronous code under the
hood in the next section.
Mentioned an optional section on promises and async/await later in the course.
· Overall Goal:
Emphasized the need for asynchronous, non-blocking code to build highly performant
and scalable web applications in Node.js.
Acknowledged the challenges of callback hell and introduced more advanced tools as
potential solutions.
node will start reading the file in the background and as soon as it is ready it
will start the callback function, the callback function takes two arguments
the one is error and the second one is actual data
fs.writeFile("./txt/final.txt", `${data2}\n$
{data3}`, "utf-8", (err) => {
console.log("Your files has been written🥳");
});
});
});
});
console.log("Will read file!");
incase of error changing the first callback function , just mistyping startttt
// Non-blocking, Asynchronous code
fs.readFile("./txt/starttttt.txt", "utf-8", (err, data1)
=> {
if (err) return console.log("ERROR!💀");
fs.writeFile("./txt/final.txt", `${data2}\n$
{data3}`, "utf-8", (err) => {
console.log("Your files has been written🥳");
});
});
});
});
console.log("Will read file!");
Creating a
simple web
server
Capable of accepting request and capable of
sending back responses..
Output
Routing
Routing means implementing different actions for
different urls.
Http header is basically a piece of information
about the response that we are sending back.
Implementing Routing
in this server
Requesting from URL .
Deleting Packages
Npm unistall express(whatever the
package name is)
Sharing the code without the node
modules
All we have to do is npm install
Suppose we have a file without the node
modules folder , all we have to do is npm
install.
Introduction to
Backend
Development
How the web works behind the scenes ?
Lets say we want to access google
maps by writing
www.google.com/maps into the
browser.
Need to know
1. The domain name like google.com
is not actually the real address of the
server which we are trying to access.
2. Browser need to convert the
domain name to the real address of
the server and that happens through
DNS .
3. DNS stands for domain name
server which are special server that
are basically like the phonebook of the
internet.
4. So the first thing that happens
when we opens a website is the
browser makes request to the DNS.
5. Then the DNS server matches the
address of the domain name that we
typed in the browser to the server real
address and this happens through ISP.
6. Once we have the real address A
tcp connection is established between
the browser and server which is now
finally connected.
And this connection is kept alive for
the entire time it takes to transfer all
the files of the website.
What is TCP/IP ?
Back-end Development:
Back-end Stack:
Scope of NodeJS:
Conclusion:
These points highlight the distinction between static and dynamic websites,
the role of APIs in modern web development, and the significance of Node.js
in building dynamic, data-driven applications. Understanding these concepts
is crucial for proficient back-end web development.
How Node.js
works behind
the scenes
1. Node.js Dependencies:
Node.js runtime relies on several dependencies to function
properly.
The two primary dependencies are:
V8 Engine: Developed by Google, V8 is crucial for
interpreting JavaScript code and converting it into machine
code.
libuv: An open-source library focused on asynchronous I/O
operations, providing Node.js access to the underlying
operating system, file system, networking, etc.
2. V8 Engine:
Responsible for converting JavaScript code into machine code
understandable by the computer.
Fundamental part of Node.js architecture, enabling it to execute
JavaScript code.
3. libuv:
Written in C++, libuv facilitates asynchronous I/O operations.
Implements critical features like the event loop and thread pool,
essential for Node.js functionality.
Handles tasks such as executing callbacks, network I/O, file
access, and more.
Provides a layer of abstraction for developers, making it easier to
interact with low-level system functionalities.
4. Event Loop and Thread Pool:
Event loop: Manages tasks like executing callbacks and handling
network I/O.
Thread pool: Handles heavier tasks such as file access and
compression.
These concepts will be elaborated on in subsequent lectures.
5. Node.js:
Written in both C++ and JavaScript, Node.js amalgamates
various libraries into a cohesive runtime environment.
Offers developers access to low-level functionalities through
high-level JavaScript APIs.
Provides a seamless experience for developers, abstracting away
complexities of underlying C++ code.
6. Additional Dependencies:
Apart from V8 and libuv, Node.js relies on other libraries for
specific tasks such as HTTP parsing, DNS requests,
cryptography, and compression.
While these dependencies are crucial for Node.js functionality,
they are less significant for understanding the overall
architecture.
7. Conclusion:
Node.js seamlessly integrates various dependencies to offer
developers a robust server-side runtime environment.
Developers can write pure JavaScript code while leveraging the
underlying C++ implementations for efficient execution.
Understanding the architecture enables developers to harness
Node.js effectively for building server-side applications.
Node Process
and threads
When we use node on a computer
it means a node process is
running on that computer.
And a process is just a program in
execution and node js is just a c+
+ program.