Introduction to Node.js

What is Node.js and Why it is used?

Node.js is an asynchronous event-driven JavaScript run-time built on Chrome’s V8 JavaScript engine.

Node.js is famous for the non-blocking, asynchronous and network programming. Node.js is a popular choice to create web server these days for the responsive UI websites.

“Node. js” is also called as a server-side “JavaScript” platform. Its creator Ryan Dahl introduced it during 2009 JSConf.eu Conference. Contributions from the community resulted in evolution of Node.js. Also, its sponsor cloud computing company Joyent contributed for its successful growth.

3 Building Blocks of Node.js

“Node” is comprised of three building blocks.

  1. A cross-platform and event-driven IO library called as “Lib UV” which was actually built to port Node.js to Windows environment. It replace many Unix specific libraries so that it is compatible with Window environment.
  2. The “JavaScript” engine developed by Google called as “V8” for its Chrome web browser. All of the Node.js releases made sure that it is within the realm of Google’s updated “V8” by leveraging Google’s innovations to V8.
  3. Customized C++ and JavaScript code specific to Node.js

What is V8 JavaScript Engine?

  • V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
  • It is used in Chrome and in Node.js. V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs.
  • When you run a program, V8 engine compiles the JavaScript code using Just in-time (JIT) compilation to make it understandable for OS.

What is JavaScript Run-time?

  • A JavaScript run-time is a platform where JavaScript programs can be executed.
  • Node.js is an example of JavaScript run-time where you can run JavaScript code on Node.js without requiring the browser.

Is Node.js used for frontend or backend?

The short answer is, it is used for frontend, backend and standalone programs as well. Node.js is based on JavaScript’s V8 engine, but it doesn’t mean that it is only for frontend programming. Node.js effectively uses the V8’s single thread processing, which makes it very lightweight network programming language. What it basically does is that it delegates blocking operations to the host operating system, which is the reason why it performs well even being single threaded programming language.

Is Node.js easier than Java?

Since Node.js adapts JavaScript’s ECMAScript syntax, if you know JavaScript, it is easier for you to learn. In fact, if you know JavaScript you can focus on the Node.js specific concepts. But, is it easier than Java? If you know JavaScript is inspired from Java syntax, so most of the Java syntax is similar to what you learn in Node.js. However, there are differences in terms of how we implement some fundamental concepts like non-blocking programming, Promises etc.,

Getting Started with Node.js

You can get Node.js in various ways. You can download the installers from nodejs.org and install using its wizard which takes care of installation and configuring the environment variables etc., or else, you can directly download the binaries also, and configure the environment variables etc., correctly. If you would like to build it on yourself, then you can also download the source code from its repository.

You can also make use of a tool called “NVM” developed by Tim Caswell. Using nvm you can clone different versions of Node.js from its GitHub. You can use nvm to install and manage multiple Node.js versions. It is extremely helpful as it allows us to test our code with multiple Node versions.

Once you clone nvm, we can use “nvm install” and specifying the version number to install specific Node version. After installation, you can switch between different Node versions using “nvm use” command. You can also set 1 particular version as the default one by using the command “nvm alias default”.

References