Overview
- Node Js is a tool to run it as a web server to build a client server application
- It uses to Google V8 engine to execute JavaScript code
- Built upon event driven architecture
- By default, the only single thread is responsible to handle the client request, but we can run multiple threads on the same machine to achieve scalability and fault tolerance.
- In order to build an application, we need to use third party web framework like Express, Meteor etc.
Architecture
Application code gets compiled by V8 (JavaScript engine developed by google to use in Chrome). The code communicates with low-level Node.js components via bindings. All the events written in the code are registered with Node.js. Once events are triggered, they are enqueued in the event queue according to the order that they are triggered. As long as there still are remaining events in the event queue, the event loop keeps picking them up, calling their callback functions, and sending them off to worker threads for processing. Once a callback function is executed, its callback is once again sent to the event queue, waiting to be picked up by the event loop again.
Components of NodeJs
V8: V8 is Google’s open source high-performance JavaScript engine, written in C++ and used in Chromium, Node.js and multiple other embedding applications
libuv: libuv is originally developed to provide asynchronous I/O that includes asynchronous TCP & UDP sockets, (famous) event loop, asynchronous DNS resolution, file system read/write, and etc. libuv is written in C.
Other low level components
c-ares: c-ares is a C library for asynchronous DNS requests (including name resolves)
HTTP parser: This library is used to parse HTTP messages (request and response) which is written in C
OpenSSL: OpenSSL is an open source project that provides a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols.
zlib and many others.
Node.js Binding: a binding basically is a wrapper around a library written in one language and expose the library to codes written in another language so that codes written in different languages can communicate.
C/C++ Add-ons: C/C++ Addons are dynamically-linked shared objects, written in C or C++, that can be loaded into the Node.js using the require function, and used just as if they were an ordinary Node.js module. They are used primarily to provide an interface between JavaScript running in Node.js and C/C++ libraries.
Node.js API: Node.js API is an abstraction upon other low level modules and is the interface with built in libraries.
Application: Node.js application code
Node.Js suitability
- a) Suitable for
- Real time web apps (chat or other application in which connection needs to be open)
- If we need to build API’s
- Streaming Application
- An application which depends on I/O bound tasks.
- b) Not suitable for
- CPU intensive task (There is workaround to achieve CPU intensive task)
Pros and Cons
a) Pros
- Asynchronous event driven I/O helps concurrent request handling
- Use a Single Programming Language
- Delivers Improved Performance
- Easy to Scale
- Cross-platform
b) Cons
- It runs on JavaScript, which is easy to learn but very hard to write less error prone code and has no compile-time type checking
- Nested callbacks
- Lack of Robust Libraries