NodeJS_Unit_1

Introduction to Node JS

    • Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser.
    • Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.
    • NodeJS is not a framework and it’s not a programming language.
    • We often use Node.js for building back-end services like APIs like Web App or Mobile App.
    • It’s used in production by large companies such as Paypal, Uber, Netflix, Walmart, and so on.
    • Node.js also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent.
    Node.js = Runtime Environment + JavaScript Library

Features of Node.js

    1) Asynchronous and Event Driven −

    All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

    2) Very Fast −

    Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution. Node.js is asynchronous and single-threaded. This means that all I/O operations don’t block any other operations. It also means that you can send emails, read files, query the database, etc. all at the same time.

    3) Single Threaded but Highly Scalable −

    Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server.

    4) No Buffering −

    Node.js applications never buffer any data. These applications simply output the data in chunks.

What is Node JS?

    • Node.js is an open source server environment
    • Node.js is free.
    • Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).
    • Node.js uses JavaScript on the server.

Advantages of Node JS

    1. Easy Scalability:

    Developers prefer to use Node.js because it is easily scaling the application in both horizontal and vertical directions. We can also add extra resources during the scalability of the application.

    2. Fast Suite:

    NodeJs runs on the V8 engine developed by Google. Event loop in NodeJs handles all asynchronous operation so NodeJs acts like a fast suite and all the operations can be done quickly like reading or writing in the database, network connection, or file system..

    3. Easy to learn and code:

    NodeJs is easy to learn and code because it uses JavaScript. If you are a front-end developer and have a good grasp of JavaScript you can easily learn and build the application on NodeJS..

    4. Advantage of Caching:

    It provides the caching of a single module. Whenever there is any request for the first module, it gets cached in the application memory, so you don’t need to re-execute the code..

    5. Hosting:

    PaaS (Platform as a Service) and Heroku are the hosting platforms for NodeJS application deployment which is easy to use without facing any issue..

    6. Corporate Support:

    Most of the well-known companies like Walmart, Paypal, Microsoft, Yahoo are using NodeJS for building the applications. NodeJS uses JavaScript, so most of the companies are combining front-end and backend Teams together into a single unit.

Traditional Web Server Model

    In the traditional web server model, each request is handled by a dedicated thread from the thread pool.
    If no thread is available in the thread pool at any point of time then the request waits till the next available thread.
    Dedicated thread executes a particular request and does not return to thread pool until it completes the execution and returns a response.

Node.js Process Model

    • Node.js processes user requests differently when compared to a traditional web server model.
    • Node.js runs in a single process and the application code runs in a single thread and thereby needs less resource than other platforms.
    • All the user requests to your web application will be handled by a single thread and all the I/O work or long running job is performed asynchronously for a particular request. So, this single thread doesn't have to wait for the request to complete and is free to handle the next request.
    • When asynchronous I/O work completes then it processes the request further and sends the response.
    • An event loop is constantly watching for the events to be raised for an asynchronous job and executing callback function when the job completes.
    • Internally, Node.js uses libev for the event loop which in turn uses internal C++ thread pool to provide asynchronous I/O.

Working in REPL

The term REPL stands for Read,Eval, Print and Loop. It specifies a computer environment like a window console or a Unix/Linux shell where you can enter the commands and the system responds with an output in an interactive mode.

REPL Environment

The Node.js or node come bundled with REPL environment. Each part of the REPL environment has a specific work.

Read:

It reads user's input; parse the input into JavaScript data-structure and stores in memory.

Eval:

It takes and evaluates the data structure.

Print:

It prints the result.

Loop:

It loops the above command until user press ctrl-c twice.

No comments:

Post a Comment