Node.js

Sumeet Panchal
4 min readDec 28, 2020

After reading the title first thing which comes into our mind is What is Node.js?

Let me explain, Node.js is nothing but a runtime environment for executing Javascript code.

Node.js was written initially by “Ryan Dahl” in 2009, about thirteen years after the introduction of the first server-side JavaScript environment.

What was the real need?

Well before Node.js was invented, JavaScript could only run on browsers. Every browser has its own implementation of the JavaScript engine and this is the reason why JavaScript code behaves differently on different browsers.

For eg: Chrome uses the V8 engine, Microsoft uses the Chakra engine, and Firefox uses the SpiderMonkey engine.

So to take advantage of the Javascript codebase outside the browser Node.js was developed in C++ using the “Chromes v8” engine with some additional feature which is different from a browser version.

Diagram showing node.js

It has its own advantages:

  1. Great for prototyping and agile development.
  2. Super fast and highly scalable.
  3. JavaScript everywhere.
  4. Cleaner and more consistent codebase.
  5. Large ecosystem of open-source libraries.

Note: Node.js is not a framework or a programming language.

How does it work?

Node applications are “asynchronous” by default. In Node, we have a “single thread” to handle all the requests. It uses an “event queue” to handle multiple requests.

This kind of architecture makes Node ideal for “I/O intensive” applications. In contrast, the Node should not be used in “CPU intensive applications” like video encoding or image manipulations since it requires a load of calculations and hardly touches a file or network system.

Node event loop architecture using single thread.
Single-thread architecture using an event loop.

Enough theory, lets jump on how to install Node on your machine. (I am using a mac machine.)

  1. Open up your terminal.
  2. Check if you have any previous Node version installed on your machine by running a command—[node --version ].
  3. To install the latest version, open your browser and head to https://nodejs.org/en/download/.
  4. Download the stable version of Node[ in my case it's 14.15.3 ].
  5. Install the package which you have just downloaded.
  6. Go back to the terminal and run the command from step:1 and check if the latest version is installed successfully.

Awesome if you have come this far, now we are ready to start building our first new application using Node.

Buidling your first application using Node.

Note: Preferred code editor will be “Microsoft’s VS Code editor”. But you can use any editor for your ease for eg: Sublime, Atom, etc…. Below is the download link for the editor.

https://code.visualstudio.com/download.

Now create your first project by creating a folder in my case “FIRST-APP”, just like the below screenshot.

function sayHello(name) {
console.log(name);
}
sayHello('Sumeet');

Paste the above code as shown in the below illustration. In order to run the file, we need to run the command called [ node <filename> ]. So in my case, the command would be node app.js

Node.js is totally built upon a module system and it also provides a bunch of predefined modules that you can use for serving an HTTP request, handling file systems, and so on. I will pass on the link below for your reference.

https://nodejs.org/dist/latest-v14.x/docs/api/

That's awesome you have come this way and finally, you have set up Node.js on your machine. In my future blogs I will try to explain the Node Core concepts and if you like this blog please don’t forget to share your love by clapping.

Thanks a ton!!!!…

You can go through my other blogs mentioned below.

--

--