Node JS Hand Book 1654187904
Node JS Hand Book 1654187904
Node JS Hand Book 1654187904
Node.js
BIRMINGHAM - MUMBAI
What You Need To Know About Node.js
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
www.packtpub.com
About the Author
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
https://www.packtpub.com/mapt
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser.
What You Need To Know About Node.js
This eGuide is designed to act as a brief, practical introduction to Node.js. It is full
of practical examples that will get you up and running quickly with the core tasks of
Node.js.
We assume that you know a bit about what Node.js is, what it does, and why you
want to use it, so this eGuide won't give you a history lesson on the background of
Node.js. What this eGuide will give you, however, is a greater understanding of the
key basics of Node.js so that you have a good idea of how to advance after you've
read the guide. We can then point you in the right direction of what to learn next
after giving you the basic knowledge to do so.
• Cover the fundamentals and things you really need to know, rather than
niche or specialized areas
• Assume that you come from a fairly technical background and so you
understand what the technology is and what it broadly does
• Focus on what things are and how they work
• Guide you in developing an app to get you up, running, and
productive quickly
Overview
Node.js is a booming technology. It is turning out to be a popular one among the
open source developers. There is a stack of articles on the web. For any naive user,
to get the right one is important. That said, it is important for me to figure out a
heuristic way to create a guide on Node.js.
While working on the outline of this book, I presented the Node.js introduction
sessions. I found this as the best way to receive feedback about the information flow.
What inspires me about this book is the simplicity of the language and how the
reader moves from the basic building blocks of JavaScipt to Node.js.
The first section starts with the basic building blocks of JavaScipt, moving toward
Node.js concepts and how it works. It provides the conceptual clarity required for
any beginner.
After getting an overall understanding of the Node.js architecture, we step into the
creation of an application server in Node.js. This section targets beginners as well as
intermediate Node.js developers. You can actually code along with the steps and get
access to the best practices recommended in this section.
The last section is all about the keywords or terminology used in the preceding
two sections.
You can make a note of all the highlighted keywords, which can be referred to in the
last section. The structure of each keyword contains a basic description, points to
remember, supportive pseudo-code, and more sources.
Let's start this learning journey and get the best possible outcomes of it.
Table of Contents
Section 1: Applying JavaScript to the Server Side 1
Learning traditional JavaScript 1
Why does JavaScript create a single stack? 2
Callback mechanism 3
Introducing eventloop 6
Introducing Node.js 8
Eventloop revisited with Node.js 8
Single-threaded eventloop model on a server 9
Why and where is Node.js used? 10
Section 2: Building a Node.js App 12
NPM community 12
Installing Node and NPM 13
Let's code 14
Building the and configuring a server 14
Best practices 16
Picking a framework 16
Handling asynchronicity 17
Using NPM libraries 17
Debugging 17
Profiling 17
Unit testing 17
Versioning 18
Maintaining configurable settings 18
Creating API endpoints 18
Future scope and the Node.js ecosystem 29
Section 3: Node.js Cheat Sheet 30
Summary 37
[i]
Applying JavaScript to the
Server Side
The aim of this section is to introduce the basic building blocks of JavaScript and also
its core working. Further, we move toward how JavaScript forms a base scripting
language for Node.js. In this section, we will also provide the usefulness of Node.js
and its applications.
JavaScript is a popular scripting language and supports all browsers. Its usage
expanded from client side to server in 2009. With the invention of Node.js by Ryan
Dahl, JavaScript started running at the server side. The reasons for using it will be
discussed while concluding first section.
[1]
Applying JavaScript to the Server Side
};
console.log(`Output :`, getTotal([2, 2, 3]));
When the preceding code runs, the JavaScript engine creates a single callstack in an
execution context and pushes the getTotal function in our case. When the getTotal
function is called in the console.log method, the execution flows in following way:
Once the function returns the result, the local variables are disposed (garbage
collected) and the function pops out from the stack.
[2]
Section 1
Therefore, we can say that everything executes asynchronously in JavaScript. But what
in the case of multiple functions or if one function calls the other? Let's learn it next.
Callback mechanism
In this section, you will learn about callbacks and their usefulness in the
asynchronous execution of code. Callback is a chief functional programming
technique that provides the flexibility of passing a function as an argument to
another function. Consider the following example:
setTimeout(function(){
console.log(`display after sometime!`)
}, 6000)
The next snippet simply logs the output after 6 seconds. Let's pass the previous
getTotal function as a parameter. This is illustrated in the following:
console.log(`log first`);
function getData() {
console.log(`Output : `, getTotal([2, 2, 3])).
}
setTimeout(getData, 6000);
console.log(`log last`)
[3]
Applying JavaScript to the Server Side
For the preceding code, the following callstack gets created in an execution context:
The observation in the callstack shows the output in the following sequence:
The setTimeout method is an inbuilt web API or document method of any browser
that supports JavaScript. Therefore, the execution context of the setTimeout method
is the browser web API and not in the developer-defined script's context. In order to
visualize this scenario, consider the following diagram:
[4]
Section 1
Once the operation is recognized as asynchronous (which requires web API), the
method is called in a different context and the execution of the callstack continues.
Hence, the log last is printed before the setTimeout method displays the result.
This is why JavaScript code is said to be non-blocking.
[5]
Applying JavaScript to the Server Side
Once it times out in the web API stack (after 6 seconds in our case), the web API
stack pushes the code to the task queue. So, considering the previous case, we have
the following callstack:
The task queue contains every step that should be executed next in the
callstack—the order of priority is based on a First In First Out (FIFO) approach.
Introducing eventloop
An eventloop is responsible for the following:
On every event emitted in the browser, the eventloop keeps on checking for any
tasks prioritized in the queue.
When the task is enrolled in the queue and the callstack is empty and ready for
execution, the eventloop retrieves the prioritized task from the queue and performs
the execution via callstack. This happens at the JavaScript runtime. Every browser
engine uses a similar kind of mechanism to interpret the JavaScript code.
[7]
Applying JavaScript to the Server Side
Introducing Node.js
Node.js is a JavaScript runtime environment created using Chrome's v8 engine by
a programmer named Ryan Dahl. According to Ryan, popular web servers such
as Apache use multithreaded models for systems. Hence, the context switching
between threads utilizes valuable CPU time. Moreover, the code is blocked while
making any asynchronous requests or I/O operations.
[8]
Section 1
The only difference in the preceding structure is that the browser's web API is replaced
with Node.js API. The aim of Ryan Dahl was to implement a complete non-blocking
server system. JavaScript, being a single threaded language, was the best fit as the
scripting language for Node.js. This was an evolution of JavaScript at the server side.
Therefore, the JavaScript browser boilerplates and libs can be reused over server
side. This was the reason the term isomorphism came into existence with JavaScript.
The web app server is where the Node.js magic happens. Let's fuse up the single-
threaded event model web server and see how they operate together. The following
is a component based diagram of Node.js at the server side:
[9]
Applying JavaScript to the Server Side
[ 10 ]
Section 1
• Web API's services: Node.js works well for web services where a flow of
continuous data is required, such as games or simulators. However, it can
be used with databases, but performs better when the data source used is
JSON-based.
• Delayed jobs: Sometimes, the necessity for writing data eventually arises, for
example, sending an e-mail or writing data to DB, even after the response is
sent from the server. Node.js is highly recommended because it comes with
modules to support this functionality.
• Proxy: The feature of non-blocking execution stimulates the Node.js usage
as a proxy server. A node proxy server can simultaneously handle large
amounts of multiple connections and is easily configurable.
• Data streaming: Node API provides a great stream of supportive methods.
The callback mechanism of Node.js is a great way to handle the flow of
stream and operate on them at the same time.
• Minimum viable apps: The average development time required for
an application is relatively low as compared to other frameworks. The
configuration required for booting a node server is minimal.
• Monitoring and notification application: Node.js is fast and provides
real-time solutions, which makes it feasible to be used for monitoring and
notification applications. Moreover, it has a rich system-level API that plays a
crucial role in the surveillance of events and data.
• Microservices: Rather than the whole app as a single unit, dividing the
modules with respect to functionality is a microservice pattern. Microservice
pattern is not new. However, with the advent of the Internet of Things (IoT),
Node.js provides support to microservices and IoT. It enables the elements of
an application to be updated and scaled separately.
[ 11 ]
Building a Node.js App
This section will leave you with an overall idea of powerfully dealing with
Node.js development for beginners and best practices for advanced developers.
Let's start with the community that works at the roots of this technology and
provides an outstanding support.
NPM community
NPM started as a Node package manager with the advent of Node.js. Currently, it has
become a package manager not only for Node.js but for all JavaScript modules. The
overall intention of building the NPM was to share boilerplate (open-source code).
The basic working of the NPM community is demonstrated in the following diagram:
[ 12 ]
Section 2
A reusable JavaScript code is said to be a package. The developers share their own
packages on the NPM repositories. NPM provides a platform to store, browse, and
access these packages among developers.
Once we register our code with NPM, any developer can search for it and install it
as per the requirement. These features gave rise to an NPM open source community
with worldwide contributors.
Without any ado, let's start our step-by-step procedure to install Node.js and update
npm:
1. Go to https://nodejs.org.
Select the DOWNLOADS link from the menu. The link navigates to a page
where a table of operating systems versus node installer formats is displayed.
Download the Node.js setup according to the OS configuration and install it.
2. Open the Terminal and type node -v to check the installed Node.js version.
NPM is installed as a part of the Node, but it is not necessarily up to date.
This step is recommended; however, it is still optional while installation. To
perform it, refer to the following code:
npm install npm@latest -g
[ 13 ]
Building a Node.js App
You can view the options by hitting npm help json in the Terminal. However, the
options field is normally used to save the package in package.json. This can be
done by using --save.
Let's code
Once all the packages are installed and the project structure is created, we are ready
to code.
1. The first step is to import the required module in app.js. In our case, we
require the http module of the node API. We use the require function to
import a module. We also store an instance of the module in a variable so
that it can be used further. Consider the following line of code:
var http = require("http");
2. The next step is to use the createServer method of the http instance. A
port is a mandatory requirement to create a server. We can specify that in the
listen method. It is demonstrated in the following:
http.createServer(requestListener).listen(8081);
[ 14 ]
Section 2
The writehead method provides API abstraction to write the content type of
response to be sent with the status code. In the preceding snippet, the success
code 200 is used. Finally, an end method of the response instance is used to
send the data to the client.
4. That's it. Our node server is ready for launch. Go back to the Terminal and
execute the following command: node app.js, as seen in the following
screenshot:
[ 15 ]
Building a Node.js App
Best practices
Every game has its own rules. Although not mandatory, best practices are some
standards that are recommended and followed with respect to the development of
an application. Learning about them will be of great help. The following subsections
cover some recommended best practices.
Picking a framework
A framework can be said to be a predefined structure of code with some abstraction,
and using them makes the development quite fast and easily maintainable. Such
standard frameworks are already available within the node community. The only
fuss is a matter of choice.
• Configuration-based frameworks
• Syntactic/semantic style-based frameworks
• Unopinionated frameworks
All these types are explained in detail in the last sections. An example of a
framework can be Express.js, which we installed earlier. Let's apply the abstraction
in our app.js. The following comparison of codes provides some clarity on using a
framework over the core Node.js API:
[ 16 ]
Section 2
Handling asynchronicity
By default, either the callbacks or the event emitters are used to handle asynchronous
behavior in Node.js. As the complexity of the asynchronous functions in the code
increases, the usage of callbacks also increases. As a result, it simply creates a code
structure that is not easy to understand. Using too many callbacks gives rise to
callback hell and to avoid this, the promises library is used. Note that the callback hell
is not an issue or a bug. We will study callback hell and promises library in the last
section. Nowadays, RxJS is a new concept to handle asynchronous behavior.
Debugging
The callback mechanism can be overwhelmed with errors. Hence, finding the real
cause can be tricky sometimes. The node community provides some tools such as
node-inspector, which helps to manage all the debugging.
Profiling
In an app where performance throughput is the key, profiling is a necessity. Profiling
provides us with statistic information about the current memory utilization with
respect to the load or the simple execution of a function. In a JavaScript code, a
memory leak can be detected by profiling tools such as memwatch. The details about
memwatch are provided in the last section.
Unit testing
It is said that writing the unit test of a module comes prior to writing the actual
functionality of the module. Writing unit test cases makes sure you provide a
collection of tests with respect to each part of the module. Two prominent unit test
techniques are as follows:
So, a combination of both TDD and BDD techniques is applied to test the expected
results. We will be implementing both the techniques in the next subsection for
creating an API in Node.js.
Versioning
Versioning is a great technique to maintain an application over time. All the
applications in JavaScript registered with the NPM follow version. The version
control paradigm stimulates to maintain a track list of all the features, dependencies,
and fixes in a file. For instance, the change.log file in the GitHub repository is a
great example. It is really worth managing the project by versioning it.
In the previous subsections, we studied the basic building blocks of Node.js server
and the implementation of best practices; now let's start creating an application API.
[ 18 ]
Section 2
1. Create a server instance in Express and run it (we have already installed
Express during the installation process). Express.js is a simple and minimalist
framework for devising an entire web application in Node.js. For more
details and the source of Express.js, please refer last section of this guide,
Node.js Cheat Sheet. So, let's start our code in Express:
var express = require('express');
var port = 8081;
var app = express();
app.listen(port);
2. Create a route handler for getting the list data. The data storage currently
used is an in-memory array called taskList (for demo purposes). We can
use persistent storage, such as database, files, and so on. In the following
code, a callback handler is assigned to a route /list. The route name and
callback handler are parameters in the get method of the Express app
instance. Add the following code to app.js:
var taskList = [];
app.get('/list', function(req, res){
res.send(taskList);
})
3. To check the API code, we can use Postman. Postman is a Chrome extension
to send a request to a server. Once all the required information is filled as
shown in the following screenshot, hit the Send button to request. We will
receive an empty array.
[ 19 ]
Building a Node.js App
4. The next step is to add data to the array list. This can be done using the post
method, as shown in the following steps:
1. Use the post method of the Express app.
2. Extract the body from the request using the body-parser middleware.
Here is the code for pushing the data onto the list:
app.post('/task', function(req, res){
taskList.push(req.body.task);
res.send(taskList);
})
[ 20 ]
Section 2
Once completed with the coding steps, let's see how it works by requesting
our server using Postman:
5. Now, we can create an API to update and delete the data. Here are the
following structures for PUT and DELETE API endpoint implementations:
The snippet of app.put API block is as follows:
app.put('/task/:task_index', function(req, res){
var taskIndex = req.params.task_index;
taskList[taskIndex] = req.body.task
res.send(taskList);
});
[ 21 ]
Building a Node.js App
6. Here we go; our basic API endpoints are ready. Let's enhance our prototype
with some basic utility libraries for validation purposes. We will be installing
the lodash library. The lodash is a utility library that lets us efficiently
handle operations on data stores such as variables, objects and arrays. It is
installed using the following command: npm install lodash --save
Once installed, we can require the library as follows:
var _ = require('lodash');
Consider the post method API we created before. If the task received in
the body message is empty, a null value will be added to taskList. So, to
validate this we can use the isEmpty method of lodash:
app.post('/task', function(req, res){
var task = req.body.task;
if(_.isEmpty(task)){
returnres.status(422).send("Task is empty");
}
taskList.push(req.body.task);
res.send(taskList);
})
[ 22 ]
Section 2
7. Now if we review the PUT API, it needs the same kind of validation. So,
rather than putting it differently for each API, we can write the validation
functionality in a middleware and reuse it wherever desired. Writing a
middleware using Express.js is really easy. Let's consider the following
snippet:
function validation Middleware(req, res, next){
if(_.isEmpty(req.body.task)){
returnres.status(422).send("Task is empty");
}
return next();
}
We can use the following code to link the middleware to the callback
handler. Both the endpoints, that is, add task and edit task as provided in the
following contain a middleware. It is responsible for checking whether the
task is present in the body of the client request:
app.post('/task', [validationMiddleware, function(req, res){
var task = req.body.task;
taskList.push(task);
res.send(taskList);
}])
[ 23 ]
Building a Node.js App
8. Let's apply one of the best practices of maintaining the configuration details.
Including this step, we give the developer an essence of maintaining the
configuration files for different environments periodically. To work on it, let's
use NPM's config module as follows:
1. The installation command for config is as follows:
npm install config --save
9. Now is the time to write some unit tests for our app. The Mocha test suite
and Chai assertion library are popularly used. For TDD we can use the
Mocha testing framework and for BDD testing, we will be writing assertions
using Chai. Although Mocha provides the assertions functionality of BDD,
the Chai library specializes it and provides a wider set of API. It has great
online forums and discussions too.
[ 24 ]
Section 2
Here are the steps for installing and configuring our unit test suites:
3. Mocha requires the test directory and any .js file inside it as a
prerequisite, or else it gives an error. So, create a directory test
containing a file named test.js so as to write all the unit test scripts
in it, and we are left with the following directory structure:
[ 25 ]
Building a Node.js App
An it block contains the actual test scenarios that need to be coded for
specific functionality. We can write multiple nested describe and it blocks.
Up next, we need to write assertions for our POST API response. To do so, we need
an instance of the required Chai library and use the interfaces provided by Chai.js.
The following snippet provides it:
var chai = require('chai');
var expect = chai.expect;
describe('---Testing the task list api---', function() {
it('POST: Task in list', function(done) {});
})
Our test suites don't provide an API for requesting the app server. We also
need a library for this purpose. Here, we are installing the npm request modules
development dependency. Once done, let's implement the following code:
var chai = require('chai'),
expect = chai.expect,
should = chai.should();
var request = require('request');
varconfig = require('config');
//Using config module to create a local domain
GLOBAL.domain = 'http://' + config.host + ':' + config.port;
describe('---Testing the task list api---', function() {
it('GET: Task list', function(done) {
/**
* Follow the request module documentation.
* go to : https://github.com/request/request
*/
var options = {
url: domain + '/task',
headers: {
"Content-Type": "application/json"
},
json: {
task: "Hello world"
[ 26 ]
Section 2
}
};
request.post(options, function(error, response, body) {
console.log("we got response", body)
done();
});
});
})
Next, let's write some assertion test cases in our response callback of the preceding
code. Consider the following snippet to illustrate it:
request.post(options, function(error, response, body) {
//Should be conditions
response.statusCode.should.equal(200);
//Expected conditions
expect(body).to.be.a('array');
expect(body).to.include(options.json.task);
done();
});
[ 27 ]
Building a Node.js App
Now run mocha to receive and check if the test cases are passed, as shown in the
preceding screenshot. In case the response received is not as expected, or if the
response status code changes, the test case fails and prints 0 passing.
Let's try this out by sending an empty task data in options, as follows:
var options = {
url: domain + '/task',
headers: {
"Content-Type": "application/json"
},
json: {
task: ""
}
}
This gives an overall idea of the expected response and how it fails in case we pass
an invalid payload in the request. Similarly, we can write unit tests for other API
endpoints.
We can use task runners such as grunt, broccoli, gulp, and so on, to
automate the test scenarios in a project. Here the test scenarios can be
referred to as multiple describe blocks in multiple files, segregated
with respect to functionality.
[ 28 ]
Section 2
Congratulations! With these 10 steps we completed all the basic building blocks
required for the development of Node.js projects.
Using JavaScript everywhere is the next big thing. The open source JavaScript
communities are growing at a great speed. With such new technology and an
amazing community support, the possibilities of new enhancements will open up
in Node.js. Atwood's law states, "what can be written in JavaScript will eventually
be written in JavaScript." Node.js finds applications in various areas, such as mobile
operating systems, Internet of Things (IoT), building continuous activity flows,
backend as a service, and so on. Such sophisticated technology extends and grows
continuously. Some bigger giants such as NASA use Node.js for spacesuit processing
and logistics.
References: https://twitter.com/CollinEstes/status/741994459349417984.
[ 29 ]
Node.js Cheat Sheet
• [libuv]
°° @type: Built-in C library.
°° @description: The libuv library is responsible for handling the
asynchronous behavior of an event life cycle in the system. It is
written in C. It maintains a pool of threads to handle asynchronous
requests such as IO and network-related operations.
°° @References
Design overview:
http://docs.libuv.org/en/v1.x/design.html
Code overview:
https://github.com/libuv/libuv.
• [package.json]
°° @type: JSON file.
°° @description: The package.json maintains a registry of modules
installed with NPM. The name and version fields are important in the
overall document. They uniquely identify an application.
°° @related-keypoint: The packages installed without save options
are not stored in package.json for a project. For example: npm
install express --save.
°° @References
Detailed overview:
https://docs.npmjs.com/files/package.json.
[ 30 ]
Section 3
• [require]
°° @type: Global variable.
°° @description: The require is just a wrapper around Module._load.
Module._load is responsible for loading modules that are exported.
When a module is loaded for the first time, it is cached. The next
time, Module._load checks whether the module is present in the
cache. If not, it again loads a new copy. Caching helps in reducing the
file reads, and this speeds up the application significantly.
°° @pseudo-code:
//file :example.js
module.exports = function(){
console.log("Hello everyone!")
}
//Output: "Hello everyone"
require('./example')();
Detailed references:
http://fredkschott.com/post/2014/06/require-and-the-
module-system/
Overview reference:
https://nodejs.org/api/modules.html.
• [http module]
°° @type: JavaScript module.
°° @description: The http module provides all the methods required
to handle an HTTP server. We can use the http module by requiring
as follows:
°° @pseudo-code:
var http = require('http');
http.createServer([requestListener]) // returns a server
instance.
//Server instance can be further use to listen on specified
port
http.createServer([requestListener]).listen(3000)
[ 31 ]
Node.js Cheat Sheet
API overview:
https://nodejs.org/dist/latest-v6.x/docs/api/http.
html#http_http
• [expressJS][unopinionated framework]
°° @type: JavaScript module.
°° @description: Express.js is the most popular web framework used
till date. It is said to be an unopinionated framework for Node.js
because it has flexibility and support as the backbone for any kind of
web applications.
°° @pseudo-code:
var express = require('express');
var app = express();
app.listen(8081);
°° @References:
Guide overview:
https://expressjs.com/
• [request]
°° @type: Request instance.
°° @description: It occurs to us as the parameter of a callback provided
in the creation of a server or its route in Node.js. It is emitted every
time a request arrives at the server. With respect to Node.js 6, it is an
instance of the http.Server class.
°° @pseudo-code:
functionrequestListener(req, res){
console.log(" Request recieved now", req);
res.send('Hello');
}
[ 32 ]
Section 3
°° @References:
Guide overview:
https://nodejs.org/api/http.html#http_class_http_
clientrequest.
• [response]
°° @type: Response instance.
°° @description: It occurs to us as the parameter of a callback provided
in the creation of a server or its route in Node.js. Response represents
an instance to be sent back to the client. It is passed with respect to
Node.js 6. It is an instance of the http.ServerResponse class.
°° @pseudo-code:
functionrequestListener(req, res){
console.log(" responding now", res);
res.send('Hello');
}
Guide overview:
https://nodejs.org/api/http.html#http_class_http_
clientrequest.
Example overview:
http://sailsjs.org/ and http://hapijs.com/.
[ 33 ]
Node.js Cheat Sheet
Example overview:
http://koajs.com/.
• [callback hell]
°° @type: Programming paradigm.
°° @description: A calling thread will not halt while making any non-
blocking requests. This is possible due to callbacks. Callback hell is
a scenario when multiple non-blocking requests are made. As the
complexity increases, the code structure moves gradually forward
such that it is difficult to understand the logic behind it.
°° @pseudo-code:
readFileContents("sample.txt", function (text){
console.log("First log");
readFileContents("sample2.txt", function (text2){
console.log("Second log");
writeFileContents("sample3.txt", text+text2, function(){
console.log("Last log");
});
});
});
Overview references:
http://callbackhell.com/.
• [Promises]
°° @type: Library.
°° @description: Anything returned or thrown (throw error) in the
callback is not handled. Moreover, callback hell comes into picture if
the complexity of callbacks increases. To avoid all these drawbacks,
the promise library is used as a solution.
[ 34 ]
Section 3
°° @pseudo-code:
readFileContents("sample.txt", function (text){
console.log("First log");
readFileContents("sample2.txt", function (text2){
console.log("Second log");
writeFileContents("sample3.txt", text+text2, function(){
console.log("Last log");
});
});
});
°° @References:
Overview references:
http://callbackhell.com/.
• [RxJS]
°° @type: Library.
°° @description: In a scenario of asynchronous streaming data, that is,
when the data object is not in the form of a single chunk (for example:
promises, where we either receive success or catch an error), RxJS
is used. The data is considered as streams and RxJS provides an
observable to listen to the streams.
°° @pseudo-code:
Observable.create(<subScriberFunction>)
Overview references:
https://github.com/Reactive-Extensions/RxJS.
• [node-inspector]
°° @type: Library.
°° @description: This tool provides an environment for debugging the
application code written in Node.js. It uses websockets. It is fast and
reliable. It enables seting breakpoints, the inspection and display of
variables objects, and so on, step-over and step-in code, and pause
and resume code executions. In short, all the debugging features are
provided by an IDE.
[ 35 ]
Node.js Cheat Sheet
Overview references :
https://github.com/node-inspector/node-inspector.
• [nodemon]
°° @type: Library.
°° @description: This is yet another tool for monitoring the changes
and reloading the Node.js app instance, while running.
°° @References:
Overview references:
https://github.com/remy/nodemon.
• [memwatch]
°° @type: Library.
°° @description: Profiling is a highly recommended best practice
to perform during Node.js development. A majority of the server
crashes in Node.js are due to memory leaks. So, the optimization
of code with an appropriate tool is necessary. memwatch detects the
leaks and provides information related to them.
°° @pseudo-code:
memwatch.on('leak', function(info) { ... });
https://github.com/marcominetti/node-memwatch.
• [Postman]
[ 36 ]
Section 3
°° @References:
Overview reference:
http://www.github.com/a85/POSTMan-Chrome-Extension/wiki.
Summary
The sections in this guide provide a gradual learning experience from core JavaScript
to JavaScript at the server side. In the first section, we started with a simple function
in JavaScript and how a callstack is created, further moving towards the single-
threaded eventloop model. The application of such a model at the server side makes
Node.js incredible.
The last section completes the guide by referring to different terminologies and
keywords used in the Node.js community.
Congratulations on making it this far. Well, this is just the start; as it is rightly said,
"Mastery is nothing but continuous learning process". We would recommend you to
follow up the Node.js developer communities and keep the good work on.
[ 37 ]
What to do next?
If you have any feedback on this eBook, or are struggling with something we haven't
covered, let us know at customercare@packtpub.com.
Get a 50% discount on your next eBook or video from www.packtpub.com using
the code: