All About NodeJs

For Novice and Experts!!!

Welcome, here we explore the world of NodeJS and its incredible potential for web development. From beginners to experienced developers, our content has something for everyone interested in this powerful technology.

Learn all about NodeJS by following the article hands-on.

I included working code snippets and a walkthrough of every concept in NodeJs.

you might be wondering what exactly is NodeJS 🤔 and why is it important.

Let's begin!!! 🚀🚀

What Is The Need Of NodeJs?

You might be wondering if JavaScript is an adaptable language, It can be used for many applications,so why NodeJs? Let's see.

By now we all know that JavaScript is a single-threaded language.

  • JavaScript cannot do asynchronous tasks.

  • Using JavaScript, we can’t access the computer it is running on, like File System, or IO(Input/Output).

  • Through JavaScript, we execute C++ code and return results back to JavaScript.

  • Hence, NodeJs took birth to overcome the above disabilities of Javascript making it super powerful.

What is NodeJs?

In simple terms, it’s a JavaScript-free and open-source cross-platform for server-side programming that allows users to build network applications quickly.

It is built on Google Chrome's JavaScript Engine (V8 Engine).

Node.js uses an event-driven, non-blocking I/O model.

It is perfect for data-intensive real-time applications that run across distributed devices.

Installing NodeJS

If you’re using OS X or Windows, the best way to install Node.js is to use one of the installers from the Node.js download page.

If you’re using Linux, you can use the installer, or you can check NodeSource’s binary distributions to see whether or not there’s a more recent version that works with your system.

Test: Run node -v. The version should be higher than v0.10.32.

If you are stuck, check out these docs for guidance.

Now that we have it installed, let’s get right to it!

Modules In NodeJs

When you import a file inside another file, that file is called a module.

  • Node.js treats each JavaScript file as a separate module.

  • Each module has a function wrapper of its own and the code written inside one function is local to that function.

  • module. export is an object in a Node.js file that holds the exported values and functions from that module.

  • When exported, another module can import these values with the require global method.

Core Modules of NodeJS
  • Core modules are modules that are built into node.js these modules come automatically with the node.js installation.
  1. OS Module

  2. File System Module

  3. HTTP module

  • OS module: It provides API for getting information about hardware related to CPU, memory, directories, and IP addresses.

os.jpg

File System Module: The File System module provides API for interacting with the file system of the operating system.

Various actions like reading, opening, writing, and many more can be done using this module.

Reading From a file:

Fi-eKy8aMAAFwI7.jpg

Writing to a File:

Fi-eZzsakAcxGXn.jpg

HTTP Module: HTTP is one of the most powerful modules in the node.js world that we use in networking applications.

It’s a built-in Node.js module that allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

Fi-ejv5agAACkBT.jpg

NPM (Node Package Manager):

  • NPM stands for Node Package Manager

  • It is one of the multiple package managers (others include Yarn, Bower, etc.) available for Javascript.

  • It’s the default package manager for Node.js. A package manager is a collection of software tools that a developer can use to automate managing packages.

npm comes packaged with NodeJS.

How to use npm?
  • Download and install NodeJS.

  • Create a package.json file.

  • Install npm packages.

  • (NodeJS) Require module in the file.

We have to be grateful that tools like NPM exist to automate many tasks for managing our third-party packages.

What is package.json?

A package.json is a file that describes the meta-data in a project.

It Is a great way to manage project dependencies.

It Specifies the versions of dependency that the project use.

It Makes projects easy to read and shared, with fewer installation overheads.

It's found in the root directory of the NodeJS package or module.

Dependencied Vs devDependencies:

-Dependencies should contain libraries and frameworks on which you are building your project. -example: lodash, bootstrap

-devDependencies contains packages that are used during development or while creating a build.

-nodemon is one such package

FjIqO56aUAED00Y.jpg

Event-Loop :

-The event loop is a control structure that decides what a thread should be doing at any given time.

FjOAYolaAAEqRdj.jpg

Order of execution:

-Functions that are in the call stack get executed, then get popped off the call stack.

-When the call stack is empty, all queued-up tasks get executed.

-then they get popped off the call stack.

FjOCW0JaEAM27YF.jpg

Let's understand the Output:

-It first displays 1 and 2 as they go into the stack.

-The foo and boo functions are sent to the web Api's, then to the callback queue.

-They wait in the callback queue until the stack is empty. Refer to this video for more....

Async Patterns:

  • Async patterns avoid the disadvantages of synchronous js.

    Async patterns:

  1. Callbacks

  2. Promises

  3. Async-await

Callback function:

  • A callback function is passed as an argument to another function for it to be called later.

  • Callback functions have a first-class citizen kind of treatment to functions in it.

FjTdKTHacAAw5PB.jpg

Promise:

Promises are used to avoid callback hell in Node.js.

The object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

FjTdr4ZacAEF_9T.jpg

Async-Await:

Appending “async” will return a promise from a function.

If the function is returning some value, the value will be the resolved value from the promise, or else the resolved value will be "undefined".

FjTeEqxaEAA8nkE.jpg

Event-Emitters:

  • EventEmitter class is available in the Events Node.js module.

  • They are used to create and consume custom events

  • Event Emitters create a publisher-subscriber pattern in NodeJS.

  • An event emitter can raise a new event from any part of an application.

  • A listener will listen to the raised event and have some action performed for the event. The events raised by event emitters are synchronously executed by the listeners.

FjYajYxacAIZ1s6.jpg

some commonly used functions:

  1. emit():Publish an event

  2. on():Listen to an event

  3. once(): listen's to 1 event.

  4. eventNames(): Get all the active event names.

  5. removeListener(): remove a listener.

FjYa5HgaUAAXSeW.jpg

Streams In NodeJS:

-Streams are collections of data.

They are just like arrays or strings.

Streams handle end-to-end information exchange in an efficient way.

They read and process chunks of data in pieces.

For Example: services such as YouTube don’t make us download the video and audio all at once, Browser receives the video as a flow of chunks, so we can watch.

-Stream is an instance of the EventEmitter class.

They are memory and time efficient.

Types of streams:

  1. Writable: streams where we can write data.

  2. Readable: streams from which data can be read.

  3. Duplex: streams that are both Readable and Writable.

  4. Transform: streams that can modify data

Next Steps:

Follow this course for a detailed explanation:

Yay!! We reached the end, I hope you enjoyed the blog, If so don't forget to react.

Make more amazing projects on NodeJs to become powerful.

Follow me on Twitter, for daily content Shreya-Twitter.