Getting Started with NodeJs

image.png

Getting Started with Node.js

In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script.

JavaScript is one of the best programming languages available nowadays. It was developed in the year 1995 but at that time it was just a client-side language i.e. it can run over browsers only. So if one wanted to make a website can use HTML for designing, CSS to make it attractive and java-script to make it interactive.

image.png

But then a question arises if javascript is a programming language, how browser runs it, and here javascript engine comes into the picture. A JavaScript engine is a software component that executes JavaScript code. The first JavaScript engines were mere interpreters, but all relevant modern engines use just-in-time compilation for improved performance. Every browser has a javascript engine like Chrome uses V8, Edge uses Chakra and Firefox uses Spider Monkey. When V8 was developed it was very fast compared to others, and the idea of using JavaScript everywhere came and NodeJs was developed.

What is Node.js?

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment developed in the year 2009 that runs on the V8 engine and executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command-line tools and for server-side scripting-running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

Node.js represents a "JavaScript everywhere" paradigm, unifying web application development around a single programming language, rather than different languages for server-side and client-side scripts.

Further, a package manager was introduced for the Node.js environment called node packet manager (NPM). The package manager makes it easier for programmers to publish and share the source code of Node.js packages and is designed to simplify the installation, updating, and uninstallation of packages.

How does Node.js work?

Node.js works in a Non-blocking or Asynchronous way. Let’s try to understand the Asynchronous architecture. Imagine you visited a restaurant, a waiter comes to your table and takes your order and gives it to the kitchen, and moves on to serve another table while the chef is preparing your meal. So the same person can serve many different tables, this is what we call non-blocking or asynchronous architecture and this is how node applications work, here the waiter is like a thread allocated to handle a request.

image.png

Also, Node.js is an event loop single-threaded language, here event loop is what allows Node.js to perform non-blocking I/O operations and explains how Node.js can be asynchronous. The event loop allows running one thing at a time. Having said that, Node.js JavaScript code runs on a single thread. The beauty of the event loop is not of running everything in a single thread, but it’s available to “put aside” long time-consuming I/O operations to keep the execution of other instructions. This is the reason why we get fast responses even though we could have multiple users making requests to a Node.js API at the same time. .

Why Node.Js?

  • It’s a light, scalable, and open-source language platform which makes it very easy to build apps.
  • It enables Javascript everywhere concept and increases the efficiency of the development process by filling the gap between frontend and backend applications.
  • It uses a Non-blocking I/O approach, which lets us initiate parallel requests.
  • Node has NPM (Node Package Manager) which contains libraries that can solve almost all the generic development problems and also makes the development process faster and more efficient.

Conclusion

From the above discussion, we can conclude that Node.Js is a good option for less CPU-intensive applications, we can build things in Javascript top-to-bottom, even down to the database level if we use JSON storage Object database like MongoDB but much CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach.

In the next part of the blog, we will see what happens when we type a URL in the browser, further we will get into the basics of backend development using NodeJs.