Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Node.JS Guidebook: Comprehensive guide to learn Node.js
Node.JS Guidebook: Comprehensive guide to learn Node.js
Node.JS Guidebook: Comprehensive guide to learn Node.js
Ebook347 pages2 hours

Node.JS Guidebook: Comprehensive guide to learn Node.js

Rating: 0 out of 5 stars

()

Read preview

About this ebook

The Node.js Guidebook is written and designed keeping in mind readers of varies technical and academic inclinations. Every concept has been explained in detail with appropriate examples and demonstrations with images as applicable. Topics have been aligned from simple to complex for the benefit of a beginner in understanding the technology. The logic of all codes in the examples have been explained appropriately. Concepts have been described in a simple language for easy understanding along with real-world applications of the same.
Node.js Guidebook aims to introduce readers to the world of Node.js. Node.js is an open source JavaScript run-time environment that executes JavaScript on server-side. Earlier, JavaScript was used only for client-side scripting, but Node.js supports the creation of dynamic Web applications by using JavaScript for server-side scripting. Node.js has taken the world by a storm by simplifying Web application development thorough the use of readily available and pluggable modules, thus, reducing the overall development time. Whether you are a beginner or an experienced developer, you can learn to design and develop attractive and efficient Web applications using Node.js.
LanguageEnglish
Release dateDec 10, 2019
ISBN9789388176408
Node.JS Guidebook: Comprehensive guide to learn Node.js

Read more from Dhruti Shah

Related to Node.JS Guidebook

Related ebooks

Programming For You

View More

Related articles

Reviews for Node.JS Guidebook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Node.JS Guidebook - Dhruti Shah

    SECTION 1

    ■ ■ ■

    Introduction to Node.js

    Session Objectives

    Understand the concept behind Node.js.

    Understand the features of Node.js.

    Learn to work with Node.js REPL terminal.

    Learn to use Node.js with Command Line Interface.

    Learn to use Node.js with an Integrated Development Environment (IDE).

    Compare Node.js with other JavaScript based technologies.

    1.1 Introduction

    Node.js is an open-source framework based on JavaScript. It was developed by Ryan Dahl in 2009. It is designed from the JavaScript V8 Engine of Google Chrome. Node.js was released under the MIT Licence. It is freely available and its codes are written in JavaScript that work on several platforms such as Windows, Mac OS X, Linux, etc.

    [Note: Massachusetts Institute of Technology (MIT) License is a permissive free software license. It puts very limited restrictions on reuse and also provides better license compatibility.]

    Normally, JavaScript only runs in the browser but cannot run on the machine. Node.js allows the JavaScript to run on the machine. This allows access to the file system and listen to network traffic on the machine. Also, it can handle HTTP requests like a Web server and send back responses including files. It can directly access databases from JavaScript. In short, anything which can be done with PHP or ASP can now be done by using JavaScript. That is, Node.js uses JavaScript on the server.

    1.1.1 Features and Benefits of Node.js

    It provides a cross-platform runtime environment as well as a JavaScript library.

    It is lightweight and very useful for data-intensive applications that work real-time and across several devices. This is because, Node.js uses an event-driven, single-threaded, non-blocking I/O model which is very memory efficient.

    It uses asynchronous programming methodology in which the server does not have to wait for the first request to complete in order to handle another request.

    Node.js library executes the code very quickly as it is built on the V8 JavaScript Engine of Google Chrome

    Unlike other servers, Node.js does not support buffering of data. The data is displayed in chunks.

    Node.js can read, write, create, open and close files on a server.

    It can also create, add, update, and delete data from databases.

    Node.js can generate dynamic page content and collect form data from a Web page

    Node.js uses a single-thread model with event looping so that the applications work in a non-blocking manner and become highly scalable. Due to event-looping mechanism, a single thread can handle more number of requests as compared to existing servers such as HTTP server, Apache server, etc.

    [Note: Scalability of an application is measured on the basis of the number of user requests a server can handle. Highly scalable application means it can handle lot of user requests at a time]

    Following figure depicts some important parts of Node.js:

    Figure 1.1: Parts of Node.js

    1.1.2 Types of Applications that can be Developed Using Node.js

    Node.js can be used to develop a variety of applications including:

    Web and networking applications.

    Video streaming sites which are I/O intensive Web applications.

    Single-page applications.

    Data Intensive Real-time Applications (DIRT).

    JSON APIs based Applications.

    1.1.3 Asynchronous Programming with Node.js

    Normally, when a request is sent to a Web server for fetching data or files, technologies such as PHP or ASP would handle it by sending the request to the server. However, until the server does not open, read and return the data or file content to the client, it cannot handle another request. Only after the first request is completed, the next request is addressed.

    However, Node.js follows the asynchronous methodology. Here, the request is sent to the server or the computer’s file system first. While the server opens, reads and returns the data to the client, at the same time the next request is handled by Node.js. In short, the waiting time is eliminated and the application can serve multiple requests at a time.

    1.2 Set up the Node.js Environment

    For using Node.js, download and install the latest version of Node.js from the official page https://nodejs.org

    To install Node.js, double-click the Windows Installer MSI file and follow the instructions of the installation wizard. By default, the installer will install Node.js in the C:\Program Files folder.

    1.2.1 REPL Terminal

    Node.js comes bundled with the Read-Eval-Print-Loop (REPL) terminal. It is a computer environment similar to a Unix/Linux shell or Windows Console where user can enter commands on the screen and the system responds in an interactive manner.

    The Node.js REPL environment is used as follows:

    Read: This stage reads user input, converts it to a JavaScript data-structure and stores it in memory.

    Eval: This stage evaluates the JavaScript data-structure.

    Print: This stage prints/displays the result.

    Loop: This stage loops the user command until ctrl-c is pressed twice.

    Common REPL commands

    Node.js provides the below set of REPL Commands for common tasks:

    Table 1.1: REPL Commands

    To start the REPL environment, double-click the node.exe file in the Node.js installation folder as shown below:

    Figure 1.2: Node.js Installation Folder

    This will open the REPL terminal as shown below:

    Figure 1.3: Node.js REPL Terminal

    Alternately, REPL can also be started by simply running node on Linux shell or Windows console without any arguments as follows:

    $ node

    In the Windows console, type the node command as shown below.

    Figure 1.4: Using Node from Windows Command Line Interface

    The REPL Command prompt > will appear in which user can type the Node.js commands. Similarly, Node can also be run on Linux shell as shown below.

    $ node

    Type the below expression on the Node.js window and on Windows Command prompt.

    > 1+4

    5

    > 2+(10*5)–6/20

    51.7

    >

    The output of the expressions in the Node.js REPL terminal and Windows command prompt are shown in the below image.

    Figure 1.5: Output of REPL Terminal and Windows Command Line Interface

    Similarly, Linux shell can also be used to run Node.js commands.

    To use variables to store and print values in Node.js, type the expression as shown in below image.

    Figure 1.6: Using Variables in Expressions

    In the above code, if var keyword is not used, the value gets saved in the variable and is printed on the console. However, if var keyword is used, the value is stored in the variable but does not get printed on the console.

    Another way of printing the variables or values on the console is by using the console.log() method as shown in the above image.

    [Note: REPL prints undefined after certain lines. This is because, variable declaration as well as console.log() function do not return any value. ]

    To create a multi-line expression in Node.js, type the expression a shown in the image below.

    Figure 1.7: Multi-line Expression

    [Note: The is added automatically on pressing enter key after the opening bracket. This indicates that Node.js has detected the continuity of code.]

    Node.js allows using the underscore _ symbol to retrieve the last result. The image below shows the use of underscore.

    Figure 1.8: Using Underscore _ Symbol

    To view the list of current commands, use the tab key. If the tab key is pressed after a single character, a list of all functions, commands, etc. beginning with that character are displayed as shown below:

    Figure 1:9: List of Commands

    In the image, when tab key is pressed, a list of all current commands is displayed. Also, when tab key is pressed after letter ‘d’, only the commands beginning with ‘d’ are displayed.

    .help Command

    It shows all the available REPL commands. The image below shows the result of .help command.

    Figure 1.10: List of REPL Commands

    .break Command

    The .break command is used to exit from a multi-line expression. For example, it can be used to jump out of a loop before completing the code as shown in the image below:

    Figure 1.11: Using the .break Command

    The .break command allows to quit in the middle of a continued expression in case something goes wrong and it is required to start again. Similarly, .clear command can also be used to exit a multi-line expression in the same manner.

    .save and .load Commands

    The .save command allows saving the current REPL session to a file and .load command loads the content of a file in the current REPL session as shown in the below image.

    Figure 1.12: Using the .save and .load Commands

    In the figure, the .save command saves the content of the current REPL session to a file file1.js. Next, the .load command loads the content of file1.js in the current REPL session.

    To close/exit the REPL console, press ctrl-c twice. The first time after pressing ctrl+c, it will display a message as shown in the below image.

    Figure 1.13: Using the ctrl+d Command

    When ctrl+c is pressed again, the terminal closes. Alternately, ctrl+d or .exit commands can also be used

    Enjoying the preview?
    Page 1 of 1