Web Development/ NodeJS

Getting Started with Node JS✌

Understanding the concepts and architecture working behind

Avik Kundu
GDSC KIIT

--

Header

I am sure everyone might have heard of the Buzzword “Node.js” in your First-year of college if you ever planned to venture into Web development.

This article is a small introduction to the “Runtime Environment”, which will help you to get started with creating backend server for your Web Application.

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

If you are new to Web Development, you can visit this article first before moving forward:

What exactly is Node.js?

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a web browser.

We all have used Google Chrome for most of our web surfing. Most of the website uses Javascript to create responsive, interactive elements for web pages, enhancing the user experience. Unlike HTML and CSS, JavaScript requires an interpreter to interpret JavaScript code.

Every Browser has its own JavaScript engine. Google Chrome has the V8 engine, Mozilla Firefox has SpiderMonkey, Chakra for Edge, etc.

A JavaScript engine is an interpreter that executes JavaScript code. This engine takes the JavaScript code and converts it into faster machine code. Machine code is a low-level code that a computer can run without needing to interpret it.

JavaScript was created by Brendan Eich in 1995 during his time at Netscape. He had built the language solely for its use in Frontend Development. During that time Java Applets ruled Backend Development.

Wouldn’t be it better if a single language can be used for both frontend and backend development, instead of different languages?

Node.js happened to be built in the right place and time. Just some years ago, JavaScript had started to be considered as a serious language, thanks to “Web 2.0” applications (such as Facebook, Gmail, etc.) that showed the world what a modern web experience could be like.

Why Node.js?

  • Node.js uses an asynchronous event-driven, non-blocking I/O model, which makes it lightweight and efficient.
  • It uses event-loops via JavaScript’s callback functionality to implement the nonblocking I/O.
  • Everything inside Node.js runs in a single thread.
  • Node.js’ package ecosystem, npm (Node Package Manager), is the largest ecosystem of open source libraries in the world.

I/O refers to input/output. It is the most expensive operation. The system needs to wait for the I/O operations to be complete and hence it is called Blocking or Synchronous I/O. They involve waiting time which is actually the performance bottleneck of the system.

The Single-threaded concurrency model of NodeJS is based on non-blocking I/O operations. An asynchronous programming paradigm in which the flow of the program is determined by its events. For example mouse-clicks, key-presses, and incoming requests.

The JavaScript Event Loop

Internal Architecture of NodeJS

What makes Node.js so popular and efficient that it uses an event-based programming model, non-blocking I/O operations at its heart. Node takes an approach that serves all the requests from a single thread known as the Event-Loop.

When Node receives a request from a client, it would put the request in the event queue. The event loop runs indefinitely to retrieve a request from the event queue.

In one case, if the request doesn’t require a blocking I/O operation, Node will simply process and prepare the response and send it back to the client.

In another case, if the request requires a blocking I/O operation, Node will dedicate it to a worker thread and thread pool to work on the blocking I/O operation. The blocking I/O operation may be associated with a call-back function. When the blocking I/O operation is complete, the worker thread prepares the response and sends it back to the event loop which then runs the call-back function and sends the response back to the client. Therefore the main program is not blocked by the I/O operations.

So Node keeps a single-threaded Event loop for our code and everything else runs in parallel. That’s the secret why Node has such a high I/O performance.

Watch this video for more about Event Loops:

NPM: The Node Package Manager

npm is the standard package manager for Node.js.

Often, in our Web application, we need some features which are difficult to build from scratch. For example: be it an authentication tool, WebSockets, etc.

The Node Package Manager solves this problem. It stores a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

The module ecosystem is open to all, and anyone can publish their own module that will be listed in the npm repository.

Where Node.js Should Be Used

Node.js is considered to be ideal for creating a backend server for the following:

  • Streaming data
  • Single-page apps
  • Web applications
  • Chats / RTAs
  • APIs

Many companies have successfully implemented this technology and have seen high profits:

Some Recommended YouTube Videos

Here are some videos which can be followed to get started the coding —

Other Resources

Official Docs

Last Words

Node.js was built to solve the I/O scaling problem. It does really well, but for Node.js there’s a specific meaning to that: Node.js makes good use of a single core of a processor when handling I/O bound tasks, more so than many other techniques.

But when you want to use more cores, or when you want more systems running a Node.js application, you have to fall back on more traditional scaling: Running the Node.js application on many cores or systems and putting a load-balancing web server in front of it to distribute that workaround. So while it is possible to scale Node.js across networks, developers end up implementing the same web server infrastructure as they would with any other underlying backend technology. That said, Node.js’ efficient exploitation of each core could lead to less need for so much distributed infrastructure.

So that’s all for now. I’m sure, now you have a basic idea of Node.js. It may seem that there’s too much theory, but it’s essential to know how does a technology work before diving directly into the coding part. This will make your journey as a developer more exciting!

Next time, I will come up with the practical hands-on session where we will be creating our first Web Application powered by Node.js. Follow me to get notified.

You can reach out on my Twitter, Instagram or on LinkedIn if you need more help. I would be more than happy.

Good Luck 😎 and happy coding 👨‍💻

--

--

Avik Kundu
GDSC KIIT

Software Engineer @ Red Hat | Learning, Sharing & Contributing to the Open Source