Node.js

Nodejs is a javascript runtime environment to execute javascript code. Its powered-up by V8 engine – which is used for Chrome browser. This code was written in C++ and few javascript code to full function.

Imagine, when you work in PHP, C# or Java server side development, you use Apache, IIS or Tomcat server to run your code. Nodejs also does same thing here. You write server side code using JavaScript and first Nodejs convert that code into machine code– which is low level code that machine can understand without interpreter, then process each logic base on user request. In order to do that, V8 engine had been done few modification for listening to network request, process and respond to end-user.

How to setup the Environment.

You can download installer, binaries or source code from here.


If you are using installer, you don’t need to worry about setting up environment variable. When execute this command node -v in your command or terminal window, it will show version number of running NodeJs.


However, if you are using binaries, you have to run node binary from bin folder or else you can set bin folder as environment variable in your operating system. It’s as below.

Binaries folder
Run Nodejs binary on Terminal

Last option, you can build NodeJs binaries base on your compute environment, for that you have to install C++ and Python in your machine and run build command in source code. Binaries will be in Release folder.
if you are interest to build in your local machine, read this article.

Run Simple HTTP server.

Minimum code to up and run a server as below.

var http = require('http');

http.createServer(function (req, res) {
  res.end('Hello World!');
}).listen(8080);

As you can see here, http module is in-build Addon to listen to network though post 8080. http addon is written in C++ and there is another wrapper to communicate with JavaScript and C++.

Since V8 engine is single threaded and non-blocking I/O, we can expect same behavior in NodeJs as well. In NodeJs all I/O operation are non-blocking, so it has great performance compare to other web servers.

Single thread mean, one command at a time. You can easily understand it when look at this video.

Cluster Addon.

Now NodeJs supports for cluster environment, so that you can execute your code in different CPU cores at same time. Read more about cluster in here.

C++ Addones

Addone support you to write CPU or Memory intensive code write in C++ and add into V8 engine. So that login will execute as native/assembly. Read more about C++ Addones in here.

Events

Events are basic fundamental element of NodeJs and we call Event-driven Architecture. You can read more in here. Every state change happens base on events. There are two type of events.

  • System Events: C++ core from a library called libuv.
  • Custom Events: JavaScript core

npm

npm stand for Node Package Manager and it’s npm package itself written in JavaScript. New version has npx which is a package runner. You can read more about this in here.

Node Modules

Packages are installed into the node_modules folder when installing locally, this means that you can require("packagename") to load its as main module.

Leave a Reply