ShahStavan / Learn_Node

Learn all about Node JS :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Learn Node From Scratch| with Mr.Shah

Table of Contents

  1. What is Node JS?
  2. What are features of Node JS?
  3. How Node works behind the hood?
  4. What are Asynchoronous-Callbacks and Promises in Node JS?
  5. What are installation and setup steps of Node JS?
  6. Writing first Node JS code!!!😎
  7. All that glitters isn’t gold!
  8. What are Single-Threaded Applications in Node JS?
  9. What are Multi-Threaded Application in Node JS?
  10. Problem Solved?
  11. Beauty of Node JS❤️
  12. What is Software Stack?
  13. What are the differences among Angular JS, React JS, and Vue JS are significant, with each providing unique advantages and limitations. The selection of a framework should be based on the requirements of the project.
  14. [Fetching an input from the user✌️](#Fetching an input from the user✌️)

What is Node JS?

Node.js is an open-source, cross-platform, server-side JavaScript runtime environment that is built on the Chrome V8 JavaScript engine. It allows developers to run JavaScript code outside of a web browser, enabling them to build server-side applications with JavaScript. Node.js provides an event-driven, non-blocking I/O model that makes it lightweight and efficient.

  1. What is Open-Source? Open source refers to a software development model in which the source code of a program or application is made available to the public, allowing anyone to view, use, modify, and distribute it. This means that the code can be freely shared and modified by developers around the world, without any restriction or licensing fees.

  2. What is Cross-Platform? Cross-platform refers to the ability of software or applications to run on multiple operating systems or platforms. This means that a program can be developed on one platform, such as Windows, and then run on another platform, such as Mac OS X or Linux, without requiring any changes to the code.

  3. What is Server-Side Javascript runtime environment? A server-side JavaScript runtime environment is a platform that allows developers to run JavaScript code on a server, outside of a web browser. This allows developers to create and run server-side applications using JavaScript, which was traditionally only used for client-side programming.

    One popular server-side JavaScript runtime environment is Node.js. Node.js is built on the Chrome V8 JavaScript engine and provides an event-driven, non-blocking I/O model that makes it lightweight and efficient. It allows developers to build scalable, efficient, and real-time applications using JavaScript.

What are features of Node JS?

Node.js is a popular server-side JavaScript runtime environment that provides a range of features for developers. Some of the top features of Node.js include:

  1. Non-blocking I/O: Node.js provides a non-blocking I/O model that allows it to handle a large number of requests and connections simultaneously, without blocking the event loop.

  2. Asynchronous programming: Node.js allows developers to write asynchronous code using callbacks, promises, and async/await, making it easier to handle multiple requests and perform I/O operations efficiently.

  3. Fast performance: Node.js is built on the V8 JavaScript engine, which provides fast performance and allows Node.js to execute JavaScript code very quickly.

  4. Scalability: Node.js is designed to be highly scalable, making it easy to handle large amounts of traffic and scale up or down as needed.

  5. Cross-platform compatibility: Node.js is cross-platform, meaning that it can be used on a variety of operating systems, including Windows, Mac OS X, and Linux.

  6. Large ecosystem: Node.js has a large and growing ecosystem of libraries and tools, making it easier for developers to build applications quickly and efficiently.

  7. Community support: Node.js has a large and active community of developers who contribute to the development of the platform, provide support, and create new tools and libraries to enhance the capabilities of Node.js.

Overall, these features make Node.js a popular choice for building scalable, efficient, and real-time applications using JavaScript.

How Node works behind the hood?

Node.js works behind the hood by providing a runtime environment that allows developers to run JavaScript code on the server-side. Here's a brief overview of how Node.js works:

  1. When a Node.js application is started, it creates a single thread of execution, called the event loop, which is responsible for managing all I/O operations and handling events.

    1. What is an event-loop?
      In Node.js, the event loop is a core part of the runtime environment that enables asynchronous I/O operations and handles events. The event loop is responsible for managing all I/O operations, including reading and writing to files, network requests, and database interactions.

      The event loop works by constantly checking the event queue for new events to process. When an event is added to the queue, the event loop will pick it up and handle it. Events in Node.js can be triggered by various sources, including timers, I/O operations, and user-defined events.

      One important aspect of the event loop is that it uses a non-blocking I/O model, which means that it doesn't block the execution of the program while waiting for I/O operations to complete. Instead, Node.js registers callbacks to be called when I/O operations are complete, allowing other operations to be processed in the meantime.

      The event loop is designed to be highly efficient and can handle a large number of concurrent events and I/O operations. This makes Node.js well-suited for building real-time and scalable applications, such as web servers and APIs, chat applications, and game servers.

  2. Node.js uses an event-driven, non-blocking I/O model, which means that it can handle multiple requests and connections simultaneously, without blocking the event loop.

  3. When a request is received, Node.js creates an event and adds it to the event queue. The event loop then continuously checks the event queue for new events, and processes each event in turn.

  4. Asynchronous callbacks are used to handle I/O operations, such as reading or writing files, making network requests, or interacting with databases. When an I/O operation is initiated, Node.js doesn't block the event loop, but instead registers a callback function to be called when the operation is complete.

  5. Node.js uses a single-threaded event loop, but can handle multiple concurrent connections by delegating I/O operations to the operating system kernel, which can handle multiple threads or processes.

  6. Node.js also provides a module system that allows developers to organize their code into reusable modules, which can be loaded and executed on demand.

Overall, Node.js provides a flexible and efficient runtime environment that allows developers to build scalable, efficient, and real-time applications using JavaScript.

What are Asynchoronous-Callbacks and Promises in Node JS?

Asynchronous callbacks are functions that are executed after an asynchronous operation is completed. For example, when reading a file using the fs module in Node.js, you can provide a callback function to be executed when the file has been read. This callback function will be called asynchronously, meaning that the program will continue to execute while the file is being read. Asynchronous callbacks are often used in Node.js to handle I/O operations, network requests, and other types of asynchronous operations.

Promises are another way of handling asynchronous operations in Node.js. A promise is an object that represents a value that may not be available yet. Promises can be used to handle asynchronous operations by returning a promise object instead of a callback function. The promise object represents the eventual completion of the operation, and can be used to chain multiple asynchronous operations together.

Promises have become increasingly popular in recent years due to their simplicity and ease of use. Promises are easy to reason about, and provide a more structured way of handling asynchronous operations than traditional callback functions.

Overall, asynchronous callbacks and promises are two important features of Node.js that allow developers to write efficient and non-blocking code for handling I/O operations and other types of asynchronous tasks.

What are installation and setup steps of Node JS?

  1. Download the appropriate installer for your operating system from the official Node.js website Node JS.

  2. Double-click the installer to begin the installation process.

  3. Follow the prompts in the installer to select the installation directory and other settings.

  4. Once the installation is complete, open a terminal or command prompt and type node -v to verify that Node.js is installed and working correctly. This command will display the version number of Node.js that is currently installed on your system.

  5. To start using Node.js, you can create a new JavaScript file with a .js extension and begin writing code. You can run the file using the node command in the terminal, followed by the name of the file. For example, if you have a file called example.js, you can run it using the following command: node example.js.

That's it! With Node.js installed and set up, you can now start building applications using the Node.js runtime environment and its extensive ecosystem of modules and packages.

Writing first Node JS code!!!😎

// First Code
function latefunc() {
    console.log("This will be called Asynchronously");
}

console.log("Welcome to the Node World");
setTimeout(latefunc,2000) // try to change the time parameter(second one) in settimeout function
console.log("Namaste🙏")

All that glitters isn’t gold!

The event loop is a fundamental part of Node.js and is the secret to its efficiency. However, Node.js is still limited by the amount of processing power it can harness. Complex programs that require a lot of processing significantly can slow things down. While Node.js can handle asynchronous I/O functions with ease, it is not suitable for compute-intensive applications like machine learning. Hence, it is essential to weigh the pros and cons before choosing a framework or a runtime environment.

What are Single-Threaded Applications in Node JS?

Initially for developing simple applications we generally prefer Single-Threaded applications. But, it's not the case everytime if you're hosting a website definetely developers should be ready to dealing with mutliple user requests within no time.

Single-threaded applications often get bogged down due to long processes that take time to execute. These processes can cause a block as the application needs to wait for a process to complete before moving ahead.

What are Multi-Threaded Application in Node JS?

We want multiple users to be able to access our website simultaneously. One way would be to have multiple threads, where each thread can serve a single user. Great! Problem solved? Not exactly. Let’s look at the problem again.

Each thread is serving a single user. Whether the user is reading the course or perhaps not generating any requests to the server, our idle thread is still assigned to the user.

Problem Solved?

We can serve more users now. However, as we dive deeper into back-end development, we will come across multiple ways of doing the same thing. Efficiency is essential if we want our services to scale well. In our example, we can simply add more computing resources as we need them. While this may seem like a simple solution, it comes at the cost of getting more hardware or computational resources. Let’s see if we can do something more efficient.

Beauty of Node JS❤️

Node.js truly excels in this area: Assume you want to read a file! you do not need to wait for the file to be read; instead, you can just ask the OS to work on it and call you back when it’s done reading. Conveniently, asynchronous processes use this very paradigm.

Processes, such as fetching data from storage or making new connections, can all be performed asynchronously, ensuring that the application does not get blocked. Furthermore, Node.js processes code sequentially. Without asynchronous operations, the entire program would halt until a file is read or a setTimeout() is called.

What is Software Stack?

The concept of a software stack or solution stack involves combining multiple software packages to create a comprehensive software solution. Node.js is a key component in several widely used web development stacks, all of which adhere to the principle of using JavaScript throughout the entire development process.

1. MERN STACK

- MongoDB
- Express JS
- React JS
- Node JS

2. MEAN STACK

- MongoDB
- Express JS
- Angular JS
- Node JS

3. MEVN STACK

- MongoDB
- Express JS
- Vue JS
- Node JS

What are the differences among Angular JS, React JS, and Vue JS are significant, with each providing unique advantages and limitations. The selection of a framework should be based on the requirements of the project.

AngularJS, ReactJS, and VueJS are three popular JavaScript frameworks that have their own strengths and weaknesses. Here are some differences between them:

  1. Architecture: AngularJS is a full-featured MVC framework, whereas ReactJS is a view library that focuses on building user interfaces, and VueJS is a progressive framework that can be used incrementally.

  2. Learning curve: AngularJS has a steep learning curve due to its extensive features, whereas ReactJS and VueJS are relatively easier to learn and implement.

  3. Performance: ReactJS is known for its excellent performance and speed, whereas VueJS also has good performance but is considered more lightweight than ReactJS. AngularJS can be slower due to its extensive features and complexity.

  4. Flexibility: VueJS is considered the most flexible framework, as it can be used for small, medium, or large-scale projects, whereas AngularJS is better suited for large-scale enterprise applications, and ReactJS is ideal for scalable and complex projects.

  5. Community and resources: ReactJS has a large and active community with extensive documentation and resources, whereas AngularJS and VueJS also have good communities, but not as extensive as ReactJS.

In terms of when to use each framework, it ultimately depends on the project's specific requirements, the team's expertise, and the available resources. For example, AngularJS may be a good choice for complex enterprise applications, while ReactJS may be better suited for building scalable and complex user interfaces, and VueJS may be ideal for smaller projects or for gradually implementing a new framework in an existing project.

Fetching an input from the user✌️

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Getting a single input such as name

rl.question('What is your name?', (name) => {
    console.log(`Heyy, ${name} here👋`);
    rl.close();
}
);

In Node.js, we often don’t need to use console input in our program since we are usually working with programs that have an easy-to-use front-end. However, sometimes we do need to use the console for input. For that, we have the readline module. Let’s see how that works. In the code widget below, enter your name in the text box of the >_STDIN.

Let’s break down the code and see what is happening:

  1. On line 1, we are importing the readline module so that we can use it in our code.

  2. The readline module needs an interface to work. This interface can be a file, or in our case, the console. We want to get input from the console and output some information on the console. In Node.js, the process object has two properties that can help us: the stdin for input and stdout for output. We use the createInterface method to create a new readline.Interface instance and save that as rl on line 3.

  3. We define our query on line 8. This is what will be displayed on the console.

  4. The readline.Interface has a method, which is aptly named question. This allows us to ask the user a question from the console. The question method takes a query string that we have defined before and a callback function. The callback function is passed the input as the first parameter.

  5. Our callback function is a simple console.log() with a ${answer} in backticks. The ${answer} with backticks get replaced with whatever is passed as the answer. This is done on line 11.

  6. A very important method, rl.close(), is called on line 13. This method informs the interface that we are done with our console I/O and Node.js can proceed further.

Buy Me A Coffee

Drop some ❤️ by ⭐ing repository

Share the Repository

About

Learn all about Node JS :)


Languages

Language:JavaScript 100.0%