Web Development with MongoDB and Node.js
上QQ阅读APP看书,第一时间看更新

The NoSQL movement

The term NoSQL has come to mean any kind of database that doesn't adhere to the strict structures of a typical relational database such as Microsoft SQL, MySQL, PostgreSQL, and so on. With a relational database, you are required to define ahead of time the exact structure of your schema. This means that you must have defined the exact number of columns, length, and datatype for every field in a table, and that each field must always match that exact set of criteria.

With a NoSQL database server such as MongoDB, records are stored as JSON-like documents. A typical document (record) in a MongoDB collection (table) might look like the following code:

$ mongo
> db.contacts.find({email: 'jason@kroltech.com'}).pretty()

{
    "email" : "jason@kroltech.com",
    "phone" : "123-456-7890",
    "gravatar" : "751e957d48e31841ff15d8fa0f1b0acf",
    "_id" : ObjectId("52fad824392f58ac2452c992"),
    "name" : {
        "first" : "Jason",
        "last" : "Krol"
    },
    "__v" : 0
}

One of the biggest advantages of using a NoSQL database server such as MongoDB is that it has a dynamic schema system, allowing records in a collection to be completely different from one another.

Some advantages of working with MongoDB are:

  • Dynamic schema design
  • Fast querying and indexing
  • Aggregate framework
  • Sharding and replication

In addition, as MongoDB was written using a JSON-like document structure, JavaScript becomes a powerful tool when working with queries and the interactive shell mongo. Like Node, MongoDB is also built for high performance, making it a great counterpart for building ever demanding, high traffic web and mobile applications. Depending on your exact needs, MongoDB may or may not be the right solution for your application. You should truly weigh the pros and cons of each technology before making a decision to determine which technology is right for you.

Node and MongoDB in the wild

Both Node and MongoDB are extremely popular and active in the development community. This is true for enterprises as well. Some of the biggest names in the Fortune 500 space have fully embraced Node to power their web applications. This is due in large part to the asynchronous nature of Node, which makes it a great alternative for high traffic, high IO applications such as e-commerce websites and mobile applications.

The following is just a small list of some big companies that are working with Node:

  • PayPal
  • LinkedIn
  • eBay
  • Walmart
  • Yahoo!
  • Microsoft
  • Dow Jones
  • Uber
  • New York Times

MongoDB's use in the enterprise sector is equally as impressive and wide reaching with an increasing number of companies adopting the leading NoSQL database server, such as:

  • Cisco
  • Craigslist Inc.
  • Forbes
  • FourSquare
  • Intuit
  • McAfee
  • MTV
  • MetLife
  • Shutterfly
  • Under Armour

What to expect from this book

The remainder of this book is going to be a guided tour that walks you through creating a complete data-driven website. The website we create will feature almost every aspect of a typical large-scale web development project. At its core, it will be powered by Node.js using a popular third-party framework called Express, and it will persist data using MongoDB.

In the first few chapters, we will cover the groundwork involved in getting the core of the server up and serving content. This includes configuring your environment so you are up and running with Node and MongoDB, and a basic introduction to the core concepts of both technologies. Then, we will write a web server from scratch powered by ExpressJS that will handle serving all of the necessary files for the website. From there, we will work with the Handlebars template engine to serve both static and dynamic HTML webpages. Diving deeper, we will make the application persistent by adding a data layer where the records for the website will be saved and retrieved via a MongoDB server. We will cover writing a RESTful API so that third parties can interact with your application. Finally, we will go into detail examining how to write and execute tests for all of your code.

Wrapping up, we will take a brief detour as we examine some popular, emerging frontend technologies that are becoming increasingly popular while writing single-page applications. These technologies include Backbone.js, Angular, and Ember.js.

Last but not least, we will go into details of how to deploy your new website to the Internet using popular cloud-based hosting services such as Heroku and Amazon Web Services.