Lecture 1 Notes-3250
Lecture 1 Notes-3250
js
Introduction to Node.js
What is Node.js?
● Node.js is an open-source, cross-platform JavaScript runtime.
● It runs on Chrome's V8 JavaScript engine.
● Node.js enables server-side JavaScript programming.
● Node.js helps create scalable and high-performance web applications.
Runtimes
A runtime is an environment that allows a programming language to execute code. It
provides the necessary resources and tools for a language to interact with the
operating system and hardware. Regarding JavaScript, the browser serves as the
runtime environment, and NodeJS works as runtime on the server.
There are several browser runtimes ( Javascript engine ) for JavaScript, such as V8
for Google Chrome, SpiderMonkey for Mozilla Firefox, Chakra for Microsoft Edge,
and JavaScriptCore for Apple Safari. Each of these engines has its way of
implementing and optimizing JavaScript.
Setting up Node
Steps to Install Node.js
1. Go to nodejs.org, the official Node.js website. Link
2. Choose between the LTS (Long Term Support) or the Current version of
Node.js.
3. It is recommended to select the LTS version for better stability, especially for
beginners.
4. Download the LTS installer that matches your operating system.
5. Follow the installation steps.
6. After the installation process, Node.js will be installed and ready to use.
7. To check the Node.js installation, open the terminal/command prompt.
8. Type node -v and press enter.
9. The installed version will be displayed.
setTimeout(() => {
console.log('Timer finished.')
}, 5000)
console.log('Finished timer.')
In this example, the setTimeout function doesn't block the execution. Instead, it takes
a callback function that gets executed after a specified delay.
How does NodeJS work?
Event Loop
The Event Loop is a crucial component of Node.js, responsible for enabling
non-blocking I/O operations in a single-threaded architecture. Essentially, it is a
continuous loop that checks for pending tasks and executes them one by one in the
order they were added to the queue. This process continues until there are no more
tasks left in the queue. By allowing Node.js to perform I/O operations
asynchronously, the Event Loop helps to ensure that the application remains
responsive even when handling a large number of requests.
I/O Operations
Refer to the tasks that involve reading or writing data to external resources like files,
databases, or network connections. As these operations are generally
time-consuming, Node.js manages them asynchronously to prevent the main thread
from being blocked.
● Node.js uses a combination of the Event Loop, worker threads, and callback
functions to handle I/O operations.
● When an I/O operation is initiated, Node.js sends the task to a worker thread,
which is separate from the main thread.
● The worker thread handles the I/O operation in the background, allowing the
main thread to continue executing other tasks.
● Once the I/O operation is complete, the worker thread adds a callback
function associated with the operation to the Event queue.
● The Event Loop executes the callback function when it becomes available,
allowing Node.js to handle the result of the I/O operation asynchronously.
Performance:
Pros
● Node.js is excellent for I/O-bound operations.
● Non-blocking, the event-driven architecture enables it to handle many
simultaneous connections.
Cons
● It may struggle with CPU-bound tasks.
● Single-threaded execution of JavaScript can cause decreased
performance for complex calculations and data processing.
What is a Server?
How Web Applications Work?
Web applications follow a client-server architecture, where the client sends requests
to the server. The server processes the request and sends a response back to the
client. This communication between the client and server happens via HTTP
requests and responses.
Server:
A server is a computer or software that provides resources or services to other
computers over a network. In web applications, servers store and process data,
handle user authentication, and execute server-side code. Servers are responsible
for serving static files to the browser for rendering web pages, as well as receiving
and processing user input to enable dynamic updates on the page.
Creating a Server
To create a server in Node.js, use the built-in 'http' module.
With this code, we've created a basic server that listens on port 3000 and sends
back a "Hello World!" message for every request.
Understanding Ports
A port is a unique address that identifies a process or service. Each application has
a unique port number assigned to it. When running multiple servers on a single
computer, using a different port number for each server allows the client to know
which server to communicate with.
1. First, create an HTML file named index.html with some primary content:
2. Now, modify the server that serves this file:
const http = require('http')
const fs = require('fs')
server.listen(3100)
In this example, we are using the fs.readFileSync method to read the index.html
file, and it can read files and provide content to you.
Summarising it
Let’s summarise what we have learned in this module:
● Introduction to Node.js.
● Understanding the runtime and how it works
● Advantages of using event-driven non-blocking I/O model
● Installation of Node.js
● Differences between blocking and non-blocking code
● Role of the Event Loop, worker threads, and callback functions in
handling I/O operations
● Communication between web applications and servers
● Creating a server using the http module
● Sending text and HTML as a response to an HTTP request
Some Additional Resources: