Book Image

MEAN Web Development

By : Amos Q. Haviv
Book Image

MEAN Web Development

By: Amos Q. Haviv

Overview of this book

The MEAN stack is a collection of the most popular modern tools for web development; it comprises MongoDB, Express, AngularJS, and Node.js. Starting with MEAN core frameworks, this project-based guide will explain the key concepts of each framework, how to set them up properly, and how to use popular modules to connect it all together. By following the real-world examples shown in this tutorial, you will scaffold your MEAN application architecture, add an authentication layer, and develop an MVC structure to support your project development. Finally, you will walk through the different tools and frameworks that will help expedite your daily development cycles. Watch how your application development grows by learning from the only guide that is solely orientated towards building a full, end-to-end, real-time application using the MEAN stack!
Table of Contents (18 chapters)
MEAN Web Development
About the Author
About the Reviewers

Installing MongoDB

For MongoDB's stable versions, the official MongoDB website supplies linked binaries that provide the easiest way to install MongoDB on Linux, Mac OS X, and Windows. Notice that you need to download the right architecture version for your operating system. If you use Windows or Linux, make sure to download either the 32-bit or 64-bit version according to your system architecture. Mac users are safe to download the 64-bit version.


The MongoDB versioning scheme works in such a way that only even version numbers mark stable releases, and so versions 2.2.x and 2.4.x are stable, while 2.1.x and 2.3.x are unstable releases and should not be used in production. The latest stable version of MongoDB is 2.6.x.

When you visit the download page at, you'll be offered a download of an archive containing the binaries you need to install MongoDB. After downloading and extracting the archive file, you will need to locate the mongod binary, which is usually located in the bin folder. The mongod process runs the main MongoDB server process, which can be used as a standalone server or a single node of a MongoDB replica set. In our case, we will use MongoDB as a standalone server. The mongod process requires a folder to store the database files in (the default folder is /data/db) and a port to listen to (the default port is 27017). In the following subsections, we'll go over the setup steps for each operating system; we'll begin with the common Windows installation process.


It is recommended that you learn more about MongoDB by visiting the official documentation at

Installing MongoDB on Windows

Once you have downloaded the right version, unpack the archive file, and move the folder to C:\mongodb. MongoDB uses a default folder to store its files. On Windows, the default location is C:\data\db, so in the command prompt, go to C:\ and issue the following command:

> md data\db


You can tell the mongod service to use an alternative path for the data files using the --dbpath command-line flag.

Once you've moved the MongoDB files to the right folder and finished creating the data folders, you'll get two options while running the main MongoDB service.

Running MongoDB manually

To run MongoDB manually, you will need to run the mongod binary. So, open the command prompt and issue the following command:

> C:\mongodb\bin\mongod.exe

The preceding command will run the main MongoDB service that starts listening to the default 27017 port. If everything goes well, you should see a console output similar to the following screenshot.

Running the MongoDB server on Windows

Depending on the Windows security level, a security alert dialog, which notifies you about the blocking of some service features, will be issued. If this occurs, select a private network and click on Allow Access.


You should be aware that the MongoDB service is self-contained, so you can alternatively run it from any folder of your choice.

Running MongoDB as a Windows Service

The more popular approach is running MongoDB automatically after every reboot cycle. Before you begin setting up MongoDB as a Windows Service, it's considered good practice to specify a path for the MongoDB log and configuration files. Start by creating a folder for these files by running the following command in your command prompt:

> md C:\mongodb\log

Then, you'll be able to create a configuration file using the --logpath command-line flag, so in the command prompt, issue the following command:

> echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg

When you have your configuration file in place, open a new command prompt window with administrative privileges by right-clicking on the command prompt icon and clicking on Run as administrator. In the new command prompt window, install the MongoDB service by running the following command:

> sc.exe create MongoDB binPath= "\"C:\mongodb\bin\mongod.exe\" --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB 2.6" start= "auto"

If the service was successfully created, you will get the following log message:

[SC] CreateService SUCCESS

Notice that the install process will only succeed if your configuration file is set correctly and contains the logpath option. After installing your MongoDB service, you can run it by executing the following command in the administrative command prompt window:

> net start MongoDB


Downloading the example code

You can download the example code files for all the Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed to you.

Be aware that the MongoDB configuration file can be modified to accommodate your needs. You can learn more about it by visiting

Installing MongoDB on Mac OS X and Linux

In this section, you'll learn the different ways of installing MongoDB on Unix-based operating systems. Let's begin with the simplest way to install MongoDB, which involves downloading MongoDB's precompiled binaries.

Installing MongoDB from binaries

You can download the right version of MongoDB using the download page at Alternatively, you can do this via CURL by executing the following command:

$ curl -O

Notice that we have downloaded the Mac OS X 64-bit version, so make sure you alter the command to fit the version suitable for your machine. After the downloading process is over, unpack the file by issuing the following command in your command-line tool:

$ tar -zxvf mongodb-osx-x86_64-2.6.4.tgz

Now, change the name of the extracted folder to a simpler folder name by running the following command:

$ mv mongodb-osx-x86_64-2.6.4 mongodb

MongoDB uses a default folder to store its files. On Linux and Mac OS X, the default location is /data/db, so in your command-line tool run the following command:

$ mkdir -p /data/db


You may experience some troubles creating this folder. This is usually a permission issue, so use sudo or super user when running the preceding command.

The preceding command will create the data and db folders because the –p flag creates parent folders as well. Notice that the default folder is located outside of your home folder, so do make sure you set the folder permission by running the following command:

$ chown -R $USER /data/db

Now that you have everything prepared, use your command-line tool and go to the bin folder to run the mongod service as follows:

$ cd mongodb/bin
$ mongod

This will run the main MongoDB service, which will start listening to the default 27017 port. If everything goes well, you should see a console output similar to the following screenshot:

Running the MongoDB server on Mac OS X

Install MongoDB using a package manager

Sometimes the easiest way to install MongoDB is by using a package manager. The downside is that some package managers are falling behind in supporting the latest version. Luckily, the team behind MongoDB also maintains the official packages for RedHat, Debian, and Ubuntu, as well as a Hombrew package for Mac OS X. Note that you'll have to configure your package manager repository to include the MongoDB servers to download the official packages.

To install MongoDB on Red Hat Enterprise, CentOS, or Fedora using Yum, follow the instructions at

To install MongoDB on Ubuntu using APT, follow the instructions at

To install MongoDB on Debian using APT, follow the instructions at

To install MongoDB on Mac OS X using Homebrew, follow the instructions at

Using the MongoDB shell

MongoDB archive file includes the MongoDB shell, which allows to you to interact with your server instance using the command line. To start the shell, navigate to the MongoDB bin folder and run the mongo service as follows:

$ cd mongodb/bin
$ mongo

If you successfully installed MongoDB, the shell will automatically connect to your local instance, using the test database. You should see a console output similar to the following screenshot:

Running the MongoDB shell on Mac OS X

To test your database, run the following command:

> db.articles.insert({title: "Hello World"})

The preceding command will create a new article collection and insert a JSON object containing a title property. To retrieve the article object, execute the following command:

> db.articles.find()

The console will output the following message:

{ _id : ObjectId("52d02240e4b01d67d71ad577"), title: "Hello World " }

Congratulations! This means your MongoDB instance is working properly and you have successfully managed to interact with it using the MongoDB shell. In the upcoming chapters, you'll learn more about MongoDB and how to use the MongoDB shell.