Book Image

Web Development with MongoDB and Node.js

By : Jason Krol
Book Image

Web Development with MongoDB and Node.js

By: Jason Krol

Overview of this book

This book is designed for developers of any skill level that want to get up and running using Node.js and MongoDB to build full featured web applications. A basic understanding of JavaScript and HTML is the only requirement for this book.
Table of Contents (14 chapters)
12
12. Popular Node.js Web Frameworks
13
Index

The basics of NodeJS


With the basics of JavaScript out of the way, let's focus on some of the basics of Node.

Event driven

At its core, one of the most powerful features of Node is that it is event driven. This means that almost all code you write in Node is going to be written in a way that is either responding to an event or is itself firing an event (which in turn will fire off other code listening for that event).

Let's take a look at code that we'll write in a later chapter that handles connecting to a MongoDB server using Mongoose, a popular Node.js MongoDB ODM module:

mongoose.connect('mongodb://localhost/MyApp');
mongoose.connection.on('open', function() {
console.log("Connected to Mongoose...");
});

First, we tell our mongoose object to connect to the server provided as a string parameter to the function. Connecting will take an undetermined amount of time though, and we have no way of knowing how long. So, what we do is bind a listener to the 'open' event on the mongoose.connection...