What the Hell Node JS Is

Before we start on the conceptual part of the NodeJs, I would like to share a short story that many frontend developers might have come across.

Short Story

Ritesh is a Lead Frontend Developer who got fantastic idea to set up his own startup business. He has the business plan ready with him and now he wants to meet different fund-raisers to get investment in his project so that he could actually start-up.

Before meeting with investors, Ritesh should have working prototype (demo application) to showcase project features and overall look and feel to investors. (Do not forget that Ritesh is a frontend. So, he can do certain things and but not all. Let’s differentiate…)

Ritesh Can

  • Work on branding and design related stuffs like logo, Photoshop (PSD) layouts, etc.
  • Convert Photoshop designs to HTML, CSS and JavaScript markups
  • Even create a data driven prototype using JSON
  • Marketing his product/project/service
  • Manage teams of developer

Ritesh Cannot

  • Design a database structure
  • Write code to connect a database with his application
  • Write code for the server using PHP, Java, Dotnet, Python like server-side languages

Here, “Ritesh Cannot” is one of the reason when NodeJs comes in a picture.

So, What Exactly the NodeJs Is?

NodeJs is not a magic. It is just an a JavaScript environment with different purpose. Node is very much dependent on internal or external packages. Also, the NodeJs API is pretty much tiny. So once you start learning, it’s tough to get rid of it :).

To develop a project with NodeJs, having additional framework that can use Node.js methodologies and build over it, is advisable. Express.js and Koa.js are the most popular NodeJs frameworks in this category. I recommend Express.js because it provides easy routing mechanism without much hassle.

[Tweet “#NodeJs makes it possible to handle server-side processes through your own #JavaScript”]

NodeJs Definitions

NodeJs® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, NPM, is the largest ecosystem of open source libraries in the world.
- NodeJs
In software development, NodeJs is an open-source, cross-platform runtime environment for developing server-side Web applications. Although NodeJs is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript.
- Wikipedia
#NodeJs is an environment to run #JavaScript on the #server and off the #browser — @kutec
- Kushal Jayswal

NodeJs Is

  • A server-side JavaScript environment
  • Somewhat similar to Google Chrome console
  • Another CLI Tool
  • Asynchronous, Non-Blocking
  • Single Threaded (Event Loop)
  • Non Buffering
  • Super Fast

A Server-side JavaScript Environment

JavaScript is primarily a client-side scripting language. But NodeJs has special power to make the same JavaScript available on servers too. That means we can now code entire application using JavaScript itself, doesn’t matter it’s server or client. However, NodeJs is not a framework, it’s just an environment. Because we have to do all things manually.

Let’s say, if we want to write some code for server then first we need to create server and then only we can do the further manipulation. Framework has many things pre-exist which we can use instantly to save extra human efforts in development but that’s not the case here with Node. Of course Node APIs are there and the NPM community is vast enough to expand your thoughts.

Somewhat Similar to Google Chrome Console

If you are familiar with Google Chrome browser then you must come across the developer toolbar. There are many similarity between Chrome console and NodeJs console because both are using V8 JavaScript engine. So there are many things available in both but not the all. Let’s compare few of them.

Chrome Node.js window location document global require module process console.log

Here we have a quick comparison with known JavaScript keywords. As you could see that global, require, module and process are available window, location and document are available in browser console but not in Node console. The same way global, require, module and process are available in Node console but not in browser console.

However, the console.log is the most common keyword working in both the consoles.

Another CLI Tool

Using Node. is required to be more familiar with Command Line Interface (CLI). It might be inconvenient for developers who are not used to with command line. But once you get familiar with commands, I am sure it will be more fun and faster than your usual development flow.

However, there are many addons/plugins available which you can install into your IDE. I have used Bracket’s Node.js plugin and it was great experience.

Asynchronous, Non-Blocking, Single Threaded and Event Loop

These all are technical terms and mostly concern with the performance.

Asynchronous is an execution approach which doesn’t wait to be finished and move to the other execution. So if we see traditional synchronous execution, it waits for any request to be completed and then only move to the next execution in the queue. So asynchronous is more faster.

Asynchronous uses similar approach that we have in HTML5 Web Workers for scripts.

Asynchronous Email Process
Asynchronous Email Process

Non-blocking means as and when executions happen, it will return immediately with whatever result it has without putting your thread to sleep (with high probability).

Event Loop is another approach which always waits for something to happen. Here “something” is an event. If event occurred, then we can define the next step with a callback function. Callback function is important because it give instruction for the next execution. If we don’t have callback function then it will continue iterating the same event loop infinitely. However, we can minimize the iterations using setTimeout() method.

Node Js Event Loop
Node Js Event Loop

Non Buffering

A Buffer is a chunk of memory, just like you would have it in C/C++. You can interpret this memory as an array of integer or floating point numbers of various lengths, or as a binary string. Unlike higher-level data structures like arrays, a buffer is not resizable.

Source

Super Fast

This post is more towards the theoretical concepts of the NodeJs. I am sorry if you feel bored about it. But yes, this would surely help to start with NodeJs. Working on Node projects is going to be fun.

My next post will be on — Getting Started with NodeJs.

Some Negative Factors of NodeJs

If you are going to consider NodeJs for your next project then you should be clear on the negative side of the NodeJs. Below is an interesting thread on Quora, you must read, before making any decision on the Node.

What are the disadvantages of using NodeJs?

If you have more to speak up then don’t hesitate to use comment section below.
 Happy Learning :).

Resources

  1. How To Node
  2. NodeJs — Dotnet Tricks
  3. Node School
  4. Understand Callback Function
  5. Problems with NodeJs Event Loop

Originally published at TeckStack.