What is Node.Js?-trickcode

What is Node.Js?What happens when a request comes to Node.js,Node.js Pros,Node.js Cons,How to install Node.js,Building from source,Node.js Hello World
Share it:

What is Node.Js?


Node.js is a server-side JavaScript run-time environment built on Google’s V8 JavaScript engine for building fast and scalable network applications.

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications.

The below image explains how the Node.js web server works.


What is Node.Js?



We can divide the Node.js server into mainly two parts:

  • At the front, you can see the Chrome V8 JavaScript engine (single-threaded), event loop, and other C/C++ libraries that run JavaScript code and listen to HTTP/TCP requests.
  • At the Back, libuv (includes libido) and other C/C++ libraries that provide asynchronous I/O.

What happens when a request comes to Node.js

  • Whenever a request comes to Node, the main thread(single-threaded) running in the V8 engine checks whether it is an I/O or not.
  • If it is an I/O then it immediately delegates that to the backside (kernel level) of the server where one of the threads in the POSIX thread pool actually makes Async I/O.
  • After delegating the work, the main thread is ready for accepting any new requests/events.
  • Whenever back-end threads finish the job (like fetching database records, reading file systems), the result is given to the main thread.
  • When the main thread V8 becomes free from what it is currently doing (remember it is single-threaded), it takes the result and returns it to the client.

Node.js Pros:

  • If you are a web developer, then no need to learn any other language.
  • Node.js is lightweight and faster than other server-side programming languages.
  • Node.js is single-threaded and lock-free, and it is able to handle thousands of concurrent requests.

Node.js Cons:

  • Asynchronous and event-driven code is more complex than synchronous code.
  • Modules with buggy native code can hard-crash the process. Whenever a worker dies, any requests it was handling are dropped, so one buggy API can easily degrade service for other co-hosted APIs.

How to install Node.js

Download Latest Node.js from http://nodejs.org/download/.

After installing, you are able to access node, npm from your shell. If you are unable to access then set the node.js installation path in environment variables.//windows set PATH=%PATH%;NODE_JS_BIN_PATH //tcsh shell setenv PATH ${PATH}:NODE_JS_BIN_PATH //bash shell export PATH=$PATH:NODE_JS_BIN_PATH


NODE_JS_BIN_PATH is the directory path where the “node” binary exists.

the command for checking node.js version: node –version
the command for checking npm version: npm –version

Building from source

You can download the source from Github and compile it. git clone http://github.com/ry/node.git; cd node; 

./configure; make; make install

Node.js Hello World


Create a file helloworld.js. Copy and paste the below code in helloworld.js


console.log("Hello world");
execute the code with node.

node helloworld.js
Share it:

Nodejs

What is Node.Js?

Post A Comment:

0 comments: