Book Image

Web Development with MongoDB and Node - Third Edition

Book Image

Web Development with MongoDB and Node - Third Edition

Overview of this book

Node.js builds fast, scalable network applications while MongoDB is the perfect fit as a high-performance, open source NoSQL database solution. The combination of these two technologies offers high performance and scalability and helps in building fast, scalable network applications. Together they provide the power for manage any form of data as well as speed of delivery. This book will help you to get these two technologies working together to build web applications quickly and easily, with effortless deployment to the cloud. You will also learn about angular 4, which consumes pure JSON APOIs from a hapi server. The book begins by setting up your development environment, running you through the steps necessary to get the main application server up-and-running. Then you will see how to use Node.js to connect to a MongoDB database and perform data manipulations. From here on, the book will take you through integration with third-party tools to interact with web apps. You will see how to use controllers and view models to generate reusable code that will reduce development time. Toward the end, the book supplies tests to properly execute your code and take your skills to the next level with the most popular frameworks for developing web applications. By the end of the book, you will have a running web application developed with MongoDB, Node.js, and some of the most powerful and popular frameworks.
Table of Contents (19 chapters)
Title Page
About the Authors
About the Reviewer
Customer Feedback


With the advent of ECMAscript 6, the usability of Node.JS has a great scope for future and has already been implemented today. The need of learning es6 syntactic sugar and inclusion of most of the cross-technology features inspires different technological communities to learn JavaScript.

The high performance and scalability of Node.js and open source NoSQL database solution called MongoDB cater to building fast, scalable network applications easily. This combination makes managing any form of data easy and ensures its delivery speed.

This book aims to provide different facets of web application development with Node.JS and MongoDB that is used to build an isomorphic server-side rendering web application. This books also guides us in creating a configurable Node.JS server using hapi.js and learning to develop a single-page frontend app using Angular 4.

This book will begin by introducing you to the groundwork needed to set up the development environment and a comparative study on how the new ECMAscript is different from the traditional JavaScript. Once the base is ready, we will quickly run through the steps necessary to get the main application server up and running and to learn the Node.JS core.

Further, we will move toward advanced coding techniques using controllers and ViewModels to generate reusable code that reduces development time. The development ends with learning proper testing concepts and how to automate them for reusability and maintainability.

By the end of the book, you will be connected with JavaScript ecosystem and will go through the trendy JavaScript frontend and backend frameworks.

What this book covers

Chapter 1, Welcome to JavaScript in the Full stack, provides an introduction on Node.js and MongoDB. In addition to this, it will explain the overall architecture of the application you will build using this book.

Chapter 2, Getting Up and Running, explains how to set up the development environments for Node.js and MongoDB. You will also verify that everything is set up properly by writing a sample app and running it.

Chapter 3, Node and MongoDB Basics, is about learning JavaScript at base. Further, going through need to know concepts of NodeJS and basic CRUD operations on MongoDB.

Chapter 4, Introducing Express, introduces you to the Express framework and its various components. It also walks you through organizing the basic application that you are building with this framework. It will also give you a detailed overview of the MVC components of Express.

Chapter 5, Templating with Handlebars, introduces you to the concept of using a templating engine and handlebars. Also, it shows you how to use handlebars in your application as a templating engine.

Chapter 6, Controllers and View Models, shows you how to organize the code for the sample application you build into the controllers and view of the Express framework. It will introduce you to the MVS concepts indirectly via introducing the need of separating the code into various modules and utilizing the Express framework.

Chapter 7, Persisting Data with MongoDB, shows you how to connect to the MongoDB server from the Node.js application you are building. It will also introduce you to the concept of ODM, the most popular one being Mongoose.

Chapter 8, Creating a RESTful API, introduces you to RESTful APIs. Also, it shows you the importance of RESTful wrapper for the application. Then, it will teach you how to change the current application to a REST API-based application.

Chapter 9, Testing Your Code, shows you why you need testing incorporated with your application and also what you should keep in mind for the testability of the code written in the chapter.

Chapter 10, Deploying with Cloud-Based Services, discusses the options for hosting the Node.js MongoDB application that you are building. It also compares the various PaaS solutions available in the market.

Chapter 11, Popular Node.js Web Frameworks, covers the various web frameworks available on Node.js, other than Express, which you will use to build the application in this book. You will analyze various web frameworks, such as Meteor, Sails, Koa, Hapi, and Flatiron. You will also learn one of the distinct type of framework, that is, hapi.js, in more detail by creating an API server.

Chapter 12, Single page Applications with Popular Frontend Frameworks, provides a comparative study of single-pages applications with popular frontend frameworks, such as backbone.js, ember.js, react.js, and Angular. You will learn one of the trending framework--Angular4--in detail. Also, you will analyze popular frontend aspects, such as the automation tools and transpilers available.

What you need for this book

A basic understanding of JavaScript and HTML is the only requirement for this book. However, the design of this book also helps a beginner with a basic programming knowledge and cross-platform developer to learn JavaScript and its frameworks.

Who this book is for

The book is designed for JavaScript developers with the following criteria:

  • Those who wants to learn JavaScript at backend
  • Those who knows es5 and want to start with the new ECMAscript
  • Those who has an intermediate knowledge of JavaScript and wants to explore new frameworks, such as Angular 2, hapi, and Express


Lastly, it is designed for any cross-platform developer who are keen to learn JavaScript and wanted to build interactive web applications in Node.js and MongoDB.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

In the preceding case, the setTimeout() method is provided by JavaScript (Node.js) API."

A block of code is set as follows:

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080'); 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

app.engine('Handlebars', exphbs.create({ 
    defaultLayout: 'main', 
    layoutsDir: app.get('views') + '/layouts', 
    partialsDir: [app.get('views') + '/partials'], 
    helpers: { 
        timeago: (timestamp)=> { 
            return moment(timestamp).startOf('minute').fromNow(); 

Any command-line input or output is written as follows:

$ sudo apt-get install python-software-properties
$ sudo curl -sL | sudo -E bash -
$ sudo apt-get install nodejs

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: A great alternative on the Mac is iTerm2.


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you. You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalogue of books and videos available at Check them out!


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.