Understanding the Node.js-to-AngularJS Stack Components
With the basic structure of the web framework fresh in your mind, it is time to discuss the Node.js-to-AngularJS stack. The most common—and I believe the best—version of this stack is the Node.js-to-AngularJS stack comprised of MongoDB, Express, AngularJS, and Node.js.
In the Node.js-to-AngularJS stack, Node.js provides the fundamental platform for development. The backend services and server-side scripts are all written in Node.js. MongoDB provides the data store for the website but is accessed via a MongoDB driver Node.js module. The webserver is defined by Express, which is also a Node.js module.
Figure 1.2 provides a very basic diagram of how the Node.js to AngularJS stack fits into the basic website/web application model. The following sections describe each of these technologies and why they were chosen as part of the Node.js to AngularJS stack. Later chapters in the book will cover each of the technologies in much more detail.
Figure 1.2 Basic diagram showing where Node.js, Express, MongoDB, and AngularJS fit in the web paradigm.
You can write most—or maybe even all—of your server-side code in Node.js, including the webserver and the server-side scripts and any supporting web application functionality. The fact that the webserver and the supporting web application scripts are running together in the same server-side application allows for much tighter integration between the webserver and the scripts. Also, the webserver can run directly on the Node.js platform as a Node.js module, which means it’s much easier than using, say, Apache for wiring up new services or server-side scripts.
The following are just a few reasons Node.js is a great framework:
- Event-driven scalability: Node.js applies a unique logic to handling web requests. Rather than having multiple threads waiting to process web requests, with Node.js they are processed on the same thread, using a basic event model. This allows Node.js webservers to scale in ways that traditional webservers can’t.
- Extensibility: Node.js has a great following and very active development community. People are providing new modules to extend Node.js functionality all the time. Also, it is very simple to install and include new modules in Node.js; you can extend a Node.js project to include new functionality in minutes.
- Fast implementation: Setting up Node.js and developing in it are super easy. In only a few minutes you can install Node.js and have a working webserver.
MongoDB is an agile and very scalable NoSQL database. The name Mongo comes from the word “humongous,” emphasizing the scalability and performance MongoDB provides. It is based on the NoSQL document store model, which means data is stored in the database as basically JSON objects rather than as the traditional columns and rows of a relational database.
MongoDB provides great website backend storage for high-traffic websites that need to store data such as user comments, blogs, or other items because it is quickly scalable and easy to implement. This book covers using the MongoDB driver library to access MongoDB from Node.js.
Node.js supports a variety of database access drivers, so the data store can easily be MySQL or some other database. However, the following are some of the reasons that MongoDB really fits in the Node.js stack well:
- Document orientation: Because MongoDB is document oriented, data is stored in the database in a format that is very close to what you deal with in both server-side and client-side scripts. This eliminates the need to transfer data from rows to objects and back.
- High performance: MongoDB is one of the highest-performing databases available. Especially today, with more and more people interacting with websites, it is important to have a backend that can support heavy traffic.
- High availability: MongoDB’s replication model makes it very easy to maintain scalability while keeping high performance.
- High scalability: MongoDB’s structure makes it easy to scale horizontally by sharding the data across multiple servers.
The Express module acts as the webserver in the Node.js-to-AngularJS stack. Because it runs in Node.js, it is easy to configure, implement, and control. The Express module extends Node.js to provide several key components for handling web requests. It allows you to implement a running webserver in Node.js with only a few lines of code.
For example, the Express module provides the ability to easily set up destination routes (URLs) for users to connect to. It also provides great functionality in terms of working with HTTP request and response objects, including things like cookies and HTTP headers.
The following is a partial list of the valuable features of Express:
- Route management: Express makes it easy to define routes (URL endpoints) that tie directly to the Node.js script functionality on the server.
- Error handling: Express provides built-in error handling for “document not found” and other errors.
- Easy integration: An Express server can easily be implemented behind an existing reverse proxy system, such as Nginx or Varnish. This allows you to easily integrate it into your existing secured system.
- Cookies: Express provides easy cookie management.
- Session and cache management: Express also enables session management and cache management.
- Data binding: AngularJS has a very clean method for binding data to HTML elements, using its powerful scope mechanism.
- Extensibility: The AngularJS architecture allows you to easily extend almost every aspect of the language to provide your own custom implementations.
- Clean: AngularJS forces you to write clean, logical code.
- Reusable code: The combination of extensibility and clean code makes it very easy to write reusable code in AngularJS. In fact, the language often forces you to do so when creating custom services.
- Support: Google is investing a lot into this project, which gives it an advantage over similar initiatives that have failed.