Book Image

MEAN Blueprints

By : Robert Onodi
Book Image

MEAN Blueprints

By: Robert Onodi

Overview of this book

The MEAN stack is a combination of the most popular web development frameworks available—MongoDB, Angular, Express, and Node.js used together to offer a powerful and comprehensive full stack web development solution. It is the modern day web dev alternative to the old LAMP stack. It works by allowing AngularJS to handle the front end, and selecting Mongo, Express, and Node to handle the back-end development, which makes increasing sense to forward-thinking web developers. The MEAN stack is great if you want to prototype complex web applications. This book will enable you to build a better foundation for your AngularJS apps. Each chapter covers a complete, single, advanced end-to-end project. You’ll learn how to build complex real-life applications with the MEAN stack and few more advanced projects. You will become familiar with WebSockets and build real-time web applications, as well as create auto-destructing entities. Later, we will combine server-side rendering techniques with a single page application approach. You’ll build a fun project and see how to work with monetary data in Mongo. You will also find out how to a build real-time e-commerce application. By the end of this book, you will be a lot more confident in developing real-time, complex web applications using the MEAN stack.
Table of Contents (13 chapters)
MEAN Blueprints
About the Author
About the Reviewer


When learning new technologies or even improving our existing skill set, we are always interested in full-fledged applications: how the application is built from the concept phase to an actual running application, what pain points can different technologies solve, and what guidelines we need to follow to ease our development cycle.

Speaking of technologies, probably the most widespread of them all is JavaScript. I think the good and bad part of JavaScript is its popularity. For decades, we have had no technology that is so widely spread and ready to be integrated on different platforms.

Besides running JavaScript in the browser, which we are all so used to, for a few years we can easily run our JS code on the server using Node.js. It does not stop here, as we can start developing powerful IoT projects using only JavaScript.

For many years, we saw a movement of having a single technology that dominates every platform. I think the JavaScript stack, yes stack, at the moment is big game changer. Probably, you are thinking now that it's not that perfect or colorful; yes, I cannot agree more with you, but the evolution is so rapid—and new things are pushed every day, every minute—that we are on the edge of a technological revolution.

Wait! What? Yes, we can be a part of the revolution, we can be trend setters, we can shape the future of the Web, mobile, and IoT (maybe the universe? But of course). We can push the limits and prove that we can do great things, and we can progressively improve ourselves and the people around us.

The idea behind the book

The idea behind writing this book was to provide a guide and a higher perspective of applications built using the MEAN stack. As I interact with lots of people at the company Evozon, where I'm working, I see a big desire in people to touch and feel how applications are built using different technology stacks.

Especially in popular technologies such as JavaScript, Node.js, and MongoDB. But usually, finding full end-to-end example applications is hard. Don't get me wrong; the Internet is full of great stuff, brilliant people, and so many mind-blowing things, but it is also full of noise and uncertainty, and it's hard to know which path is right or wrong and what should be done in certain scenarios.

That's why we want to showcase different scenarios of building applications using the MEAN stack. Probably it's not the only way of doing things, but it should provide you with a starting point, or give you an insight into how certain parts of an app are built.

This is not a getting started book on Node.js and Angular 2. It will jump right into action and showcase six built-from-the-beginning applications, each with different use cases and solving a high-level problem.

A little bit of twist

Writing a book takes time, a lot of energy, and a little bit of fear. I've feared that I'll never finish this book. It was a long run, and the funniest part was that the first three chapters were written in an earlier version of AngularJS. Don't worry! Everything is shipped using Angular 2.

As you might have guessed, we had a long run and rewrote everything using the currently-in-beta-release Angular 2. Why? Because, as I told you before, we want to push our limits. We want to grow and master new ways of building modern applications.

I've tried to cover a variety of applications built using the MEAN stack, ranging from a simple contact manager and a real-time chat application to a full auction website.

What this book covers

Chapter 1, Contact Manager, will cover the process of building an introductory application to save your contacts in MondoDB. The chapter will introduce you to TDD (short for Test-Driven Development) for your Node.js application. You will learn how to build an Angular 2 app that will access data from the Express API.

Chapter 2, Expense Tracker, is going to dive deep into working with monetary data in JavaScript and storing this data in MongoDB using the exact precision approach. Also, you are going to learn how to add a token for authentication of each request in the client application by extending the built-in HTTP service in Angular. Besides this, you'll see how to use the aggregation framework from MongoDB and display the result in your Angular application.

Chapter 3, Job Board, will focus on building a more consumer-level application that will enable users to define a custom profile with dynamic data. You will use reactive extensions in Angular to create different communication layers in your application. On the backend, we are going to build a RESTful API using Node.js and set up a boilerplate application from the previously built apps that we are going to use further in the book.

Chapter 4, Chat Application, will start reusing the boilerplate built in the previous chapter. But the most fun part will begin when we create a chat service layer that uses SocketIO. This will enable both the backend and the frontend Angular app to communicate in real time to send messages. The chat service will be built in such a way that it's easily extendable with new modules, besides instant messaging, such as when a user is online or goes offline.

Chapter 5, E-Commerce Application, is going to reflect the ease of storing unstructured data in MongoDB. We'll discuss in detail how to store your product catalog in a NoSQL database. Besides this, our initial architecture from the previous chapters is going to get a new form, and we'll experiment with micro-apps, each with its own responsibility. The micro-apps will use the core e-commerce module that encapsulates all the business logic. Also, this chapter will cover two client apps, one built with a totally different technology, and an admin application using Angular 2.

Chapter 6, Auction Application, is going to be more of an extension of the previous chapter; in other words, it will use the e-commerce API to fetch product information and authenticate users. This will push us not only to reuse existing code but rely on other services for faster prototyping when building products. Also, we are going to dive deeper into RxJs and see how we can build a real-time bidding system in our Angular auction application using SocketIO on the server side.

What you need for this book

You will require any modern web browser (such as Chrome's latest version or IE 10+), the Node.js platform installed on your machine, and version 3.2 or higher of MongoDB. Optionally, you can install any web server, such as Nginx, Apache, IIS, or lighttpd, to proxy requests to your Node.js application.

Who this book is for

If you are a web developer with a basic understanding of the MEAN stack, experience of developing applications with JavaScript, and basic experience with NoSQL databases, then this book is for you.


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: "Create a file called app/models/token.js."

A block of code is set as follows:

'use strict';

const LEN = 256;
const SALT_LEN = 64;
const ITERATIONS = 10000;
const DIGEST = 'sha256';
const crypto = require('crypto');

module.exports.hash = hashPassword;
module.exports.verify = verify;

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


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

    Basic authentication
      √ should authenticate a user and return a new token
      √ should not authenticate a user with invalid credentials
  2 passing

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: "It triggers an event when the Save button is clicked on and the form is submitted."


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 e-mail , 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 catalog 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 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 , and we will do our best to address the problem.