CHAPTER 10
In this chapter, we will have a look into solving common problems when combining Angular.js with the Node.js Express framework. The examples used in this chapter are based on a Contacts app to manage a list of contacts. As an extra, we use MongoDB as a backend for our contacts since it requires further customization to make it work in conjunction with Angular's $resource service.
You wish to consume a JSON REST API implemented in your Express application.
Using the $resource service, we will begin by defining our Contact model and all RESTful actions:
app.factory("Contact", function($resource) { |
We can now fetch a list of contacts using Contact.index() and a single contact with Contact.show(id). These actions can be directly mapped to the API routes defined in app.js:
var express = require('express'), |
I like to keep routes in a separate file (routes/api.js) and just reference them in app.js in order to keep it small. The API implementation first initializes the Mongoose library and defines a schema for our Contact model:
var mongoose = require('mongoose'); |
We can now use the Contact model to implement the API. Let's start with the index action:
exports.contacts = function(req, res) { |
Skipping the error handling, we retrieve all contacts with the find function provided by Mongoose and render the result in the JSON format. The show action is pretty similar, except it uses findOne and the id from the URL parameter to retrieve a single contact.
exports.contact = function(req, res) { |
As a final example, we will create a new Contact instance passing in the request body and call the save method to persist it:
exports.createContact = function(req, res) { |
You can find the complete example on GitHub.
Let’s have a look again at the example for the contact function, which retrieves a single Contact. It uses _id instead of id as the parameter for the findOne function. This underscore is intentional and used by MongoDB for its auto-generated IDs. In order to automatically map from id to the _id parameter, we used a nice trick of the $resource service. Take a look at the second parameter of the Contact $resource definition: { id: "@_id" }. Using this parameter, Angular will automatically set the URL parameter id based on the value of the model attribute _id.
You wish to use client-side routing in conjunction with an Express backend.
Every request to the backend should initially render the complete layout in order to load our Angular app. Only then will the client-side rendering take over. Let us first have a look at the route definition for this “catchall” route in our app.js:
var express = require('express'), |
It uses the wildcard character to catch all requests in order to get processed with the routes.index module. Additionally, it defines the route to use the same module. The module again resides in routes/index.js:
exports.index = function(req, res){ |
The implementation only renders the layout template. It uses the Jade template engine:
!!! |
Now that we can actually render the initial layout, we can get started with the client-side routing definition in app.js:
var app = angular.module('myApp', ["ngResource"]). |
We define route definitions to list, show and edit contacts, and use a set of partials and corresponding controllers. In order for the partials to get loaded correctly, we need to add another express route in the backend, which serves all these partials:
app.get('/partials/:name', function (req, res) { |
It uses the name of the partial as an URL param and renders the partial with the given name from the partial directory. Keep in mind that you must define that route before the catchall route, otherwise it will not work.
You can find the complete example on GitHub.
Compared to Rails, the handling of partials is explicit by defining a route for partials. On the other hand, it is quite nice to be able to use Jade templates for our partials, too.