Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with MongoDB and Node

You're reading from   Web Development with MongoDB and Node Build fast web applications for handling any kind of data

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher
ISBN-13 9781788395083
Length 330 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Joseph D'mello Bruno Joseph D'mello
Author Profile Icon Bruno Joseph D'mello
Bruno Joseph D'mello
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Welcome to JavaScript in the Full Stack FREE CHAPTER 2. Getting Up and Running 3. Node and MongoDB Basics 4. Introducing Express 5. Templating with Handlebars 6. Controllers and View Models 7. Persisting Data with MongoDB 8. Creating a RESTful API 9. Testing Your Code 10. Deploying with Cloud-Based Services 11. Popular Node.js Web Frameworks 12. Single Page Applications with Popular Frontend Frameworks

A short introduction to Node.js

One of the most important things that people get confused about while getting introduced to Node.js is understanding what, exactly, it is. Is it a different language altogether, is it just a framework on top of it, or is it something else? Node.js is definitely not a new language, and it is not just a framework on JavaScript. It can be considered as a runtime environment for JavaScript built on top of Google's V8 engine. So, it provides us with a context where we can write JavaScript code on any platform where Node.js can be installed. Anywhere!

Now, a bit about its history! Back in 2009, Ryan Dahl gave a presentation at JSConf that changed JavaScript forever. During his presentation, he introduced Node.js to the JavaScript community. He concluded it after a roughly 45-minute talk, receiving a standing ovation from the audience in the process. He was inspired to write Node.js after he saw a simple file upload progress bar on Flickr, the image-sharing site. Realizing that the site was going about the whole process the wrong way, he decided that there had to be a better solution.

Now let's go through the features of Node.js, which make it unique from other server-side programming languages.

The advantage that the V8 engine brings in

The V8 engine was developed by Google and was open sourced in 2008. As we all know, JavaScript is an interpreted language and it will not be as efficient as a compiled language, as each line of code gets interpreted one by one while the code gets executed. The V8 engine brings in an efficient model, where the JavaScript code is first interpreted and then compiled into machine-level code.

The new V8 5.9 provides a stable release that introduces TurboFan compiler which provides performance and mass optimization benefits. It also launches Ignition interpreter which is quiet efficient for all the small and big devices like servers or IOT devices etc that varies on memory spectrum. Due to such low memory footprint it delivers fast startup of an application. We can study benchmarks in following link : https://goo.gl/B15xB2

With two powerful updates, the v8 team is also working on Orinoco, which is a garbage collector that works on mechanism of parallel and concurrent compacting.

Such a high performance with promising results was the reason to push the node 8(LTS) launch date from may 2018 to october 2018. Currently we are using node 8 with a non-LTS version. It provides clean replace for users using node v4.x.x and above with no broken library. The version 8 also has various inbuilt features like buffer improvements and inbuilt promisify methods etc. We can study them in following link : https://goo.gl/kMySCS

Node.js is single-threaded!

With the advent of the web, the traditional JavaScript was intended to add simple functionality and minimal runtime in the browser. Thus, JavaScript was kept as a single-threaded scripting language. Now, just to get a brief idea regarding single-threaded model, let's consider the following diagram:

A single-threaded model creates a single Callstack in an execution context. In the preceding code, when the function getData() is invoked, the function is pushed in the stack for execution sequentially.

In the context of Node.js, JavaScript is the base scripting language, hence, Node.js is single-threaded. You might be asking, how does a single-threaded model help? Typical PHP, ASP.NET, Ruby, or Java-based servers follow a model where each client request results in the instantiation of a new thread or even a process.

When it comes to Node.js, requests are run on the same thread with shared resources. A question that is often asked is, what will be the advantage of using such a model? To understand this, we should understand the problem that Node.js tries to resolve. It tries to do asynchronous processing on a single thread to provide more performance and scalability for applications that are supposed to handle too much web traffic. Imagine web applications that handle millions of concurrent requests; if the server makes a new thread for handling each request that comes in, it will consume a lot of resources and we will end up trying to add more and more servers to increase the scalability of the application.

The single-threaded, asynchronous processing model has its advantage in the previous context, and you can process much more concurrent requests with fewer server-side resources. However, there is a downside to this approach; Node (by default) will not utilize the number of CPU cores available on the server it is running on, without using extra modules like pm2.

The point that Node.js is single-threaded doesn't mean that it doesn't use threads internally. It is just that the developer and the execution context that the code has exposure to have no control over the threading model internally used by Node.js.
If you are new to the concept of threads and process, I would suggest you go through some preliminary articles about these topics. There are plenty of YouTube videos on the same topic as well.
The following reference could be used as a starting point:
http://www.cs.ucsb.edu/~rich/class/cs170/notes/IntroThreads/

Non-blocking asynchronous execution

One of the most powerful features of Node.js is that it is both event-driven and asynchronous. So, how does an asynchronous model work? Imagine you have a block of code and at some nth line you have an operation that is time consuming. What happens to the lines that follow the nth line while this code gets executed? In normal synchronous programming models, the lines that follow the nth line will have to wait until the operation at that line completes. An asynchronous model handles this case differently.

Let us visualize this scenario with the help of the following code and diagram:

In the preceding case, the setTimeout() method is provided by JavaScript (Node.js) API. Hence, this method is recognized as synchronous and is executed in a different execution context. According to functionality to setTimeout() , it executes the callback function after a specified duration, in our case after three seconds.

Further, the current execution is never blocked for a process to complete. When Node.js API determines that the completion of an event has been fired, it will execute your callback function at that moment.

In a typical synchronous programming language, executing the preceding code will yield the following output:

If you are still interested in learning more about asynchronous models and the callback concept in JavaScript, Mozilla Developer Network (MDN) has many articles that explain these concepts in detail.
You have been reading a chapter from
Web Development with MongoDB and Node - Third Edition
Published in: Sep 2017
Publisher:
ISBN-13: 9781788395083
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime