Node.JS Guidebook: Comprehensive guide to learn Node.js
By Dhruti Shah
()
About this ebook
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.
Dhruti Shah
Dhruti Shah is an award-winning journalist with more than a decade’s experience at the BBC. She studied English at Oxford University before embarking on a career in journalism. She trained on local newspapers before making the jump to international news. She has a reputation for unearthing unusual and underreported stories stemming a wide beat. She is an Ochberg Fellow at the Dart Centre for Trauma Journalism at Columbia University, New York and a Rotary International Peace Fellow.
Read more from Dhruti Shah
100+ Solutions in Java: A Hands-On Introduction to Programming in Java (English Edition) Rating: 0 out of 5 stars0 ratings100+ Solutions in Java - 2nd Edition: Everything you need to know to develop Java applications (English Edition) Rating: 0 out of 5 stars0 ratingsBear Markets and Beyond: A bestiary of business terms Rating: 0 out of 5 stars0 ratings
Related to Node.JS Guidebook
Related ebooks
Instant MongoDB Rating: 0 out of 5 stars0 ratingsMongoose for Application Development Rating: 5 out of 5 stars5/5JAVA Programming Simplified: From Novice to Professional - Start at the Beginning and Learn the World of Java Rating: 0 out of 5 stars0 ratingsMastering Hibernate Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns Rating: 4 out of 5 stars4/5Node.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5JavaScript Regular Expressions Rating: 3 out of 5 stars3/5JavaScript for Modern Web Development: Building a Web Application Using HTML, CSS, and JavaScript Rating: 0 out of 5 stars0 ratingsThe Joy of JavaScript Rating: 4 out of 5 stars4/5Decoding JavaScript: A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition) Rating: 0 out of 5 stars0 ratingsReact Native By Example Rating: 0 out of 5 stars0 ratingsGit Essentials Rating: 4 out of 5 stars4/550 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5Getting Started with React Rating: 0 out of 5 stars0 ratingsLearning jQuery Rating: 4 out of 5 stars4/5React Components Rating: 0 out of 5 stars0 ratingsLearning JavaScript Data Structures and Algorithms Rating: 5 out of 5 stars5/5React Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsMastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5JavaScript Enlightenment Rating: 4 out of 5 stars4/5Google Maps JavaScript API Cookbook Rating: 2 out of 5 stars2/5Node.js Web Development - Third Edition Rating: 2 out of 5 stars2/5JavaScript and JSON Essentials Rating: 5 out of 5 stars5/5Test-Driven Java Development Rating: 0 out of 5 stars0 ratingsASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsModern JavaScript Applications Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratings
Programming For You
SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Godot from Zero to Proficiency (Foundations): Godot from Zero to Proficiency, #1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsAlgorithms For Dummies Rating: 4 out of 5 stars4/5Mastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5Excel 2021 Rating: 4 out of 5 stars4/5
Reviews for Node.JS Guidebook
0 ratings0 reviews
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