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.
Read more from Dhruti Shah
100+ Solutions in Java 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 ratings
Related to Node.JS Guidebook
Related ebooks
React Components Rating: 0 out of 5 stars0 ratingsAdvanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsJavaScript for Modern Web Development: Building a Web Application Using HTML, CSS, and JavaScript Rating: 0 out of 5 stars0 ratings50 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5Node.js Design Patterns Rating: 4 out of 5 stars4/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5Get Programming with Node.js Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratingsObject Oriented Programming with Angular: Build and Deploy Your Web Application Using Angular with Ease ( English Edition) Rating: 0 out of 5 stars0 ratingsMastering Node.js Rating: 1 out of 5 stars1/5Learning PHP Data Objects Rating: 5 out of 5 stars5/5Web Development with MongoDB and Node.js Rating: 0 out of 5 stars0 ratingsMastering Web Application Development with Express Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5React: Building Modern Web Applications Rating: 5 out of 5 stars5/5MEAN Web Development Rating: 5 out of 5 stars5/5Mastering JavaScript Single Page Application Development Rating: 0 out of 5 stars0 ratingsReactJS by Example - Building Modern Web Applications with React Rating: 4 out of 5 stars4/5React.js Essentials Rating: 4 out of 5 stars4/5Node.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsNode.js Web Development - Third Edition Rating: 2 out of 5 stars2/5Modern JavaScript Applications Rating: 0 out of 5 stars0 ratingsWeb Development with MongoDB and NodeJS - Second Edition Rating: 0 out of 5 stars0 ratingsNode.js By Example Rating: 2 out of 5 stars2/5JavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5AngularJS Web Application Development Blueprints Rating: 0 out of 5 stars0 ratingsJavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsNode Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsMastering React.js: Modern Web Development Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsPython Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsThe Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Teach Yourself C++ Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5The Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week 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/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom 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