Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Advanced Express Web Application Development
Advanced Express Web Application Development

Advanced Express Web Application Development: For experienced JavaScript developers this book is all you need to build highly scalable, robust applications using Express. It takes you step by step through the development of a single page application so you learn empirically.

eBook
₹799.99 ₹2621.99
Paperback
₹3276.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Advanced Express Web Application Development

Chapter 2. Building a Web API

With the foundations in place, we begin the process of building a Web API for our Vision project. We will start by setting up a persistence layer using MongoDB. We will then implement, feature-by-feature, the various aspects of our Web API.

Persisting data with MongoDB and Mongoose


MongoDB is an open source document-oriented database system. MongoDB stores structured data such as JSON-like documents, simplifying integration.

Let's start by creating a MongoDB schema for our project. The schema contains some basic information related to the project such as the project's name, a GitHub access token, a user, and a list of repositories.

Let's install Mongoose, a MongoDB Object Document Mapper for Node.js; it provides a schema-based solution to modeling your data.

npm install mongoose --save

Let's configure our application to use MongoDB and Mongoose; we add a URL for MongoDB to our configuration files ./lib/config/*.js:

{
  "express": {
    "port": 3000
  },
  "logger" : {
    "filename": "logs/run.log",
    "level": "silly"
  },
  "mongo": {
    "url":  "mongodb://localhost/vision"
  }
}

Let's create a MongoDB connection module, ./lib/db/index.js, which simply pulls in the MongoDB URL from our Winston configuration and opens a connection...

GitHub tokens


In order to acquire a GitHub token, log in to your GitHub account and go to the Accounts section of your Settings page. Here you will need to enter your password. Now click on Create new token, and name the token, if you prefer. Click on the copy to clipboard button in order to copy the token into the following login file.

Let's create a login file—./test/login.js—with the data from GitHub. We will use this in order to call the GitHub API; this will be removed in a later chapter.

module.exports = {
  user : '#USER#'
  token : '#TOKEN#'
}

Feature: Create a project


As a vision user
I want to create a new project
So that I can monitor the activity of multiple repositories

Let's add a test to our existing set of tests for our feature Create a project. This resource will POST a project to the route /project and return a 201 Created status code. The following test: ./test/project.js is the 201 Created test.

Tip

This book will not document the full set of tests for a feature. Please refer to the source code for the full set of tests.

In this example, SuperTest executes an end function that returns a response; this allows us to check the headers and body of the response.

describe('when creating a new resource /project', function(){
  var project = {
    name: "new project"
    , user: login.user  
    , token: login.token
    , repositories    : [ "12345", "9898" ]
  };

  it('should respond with 201', function(done){
    request(app)
    .post('/project')
    .send(project)
    .expect('Content-Type', /json/)
    .expect(201)
    .end...

Feature: Get a project


As a vision user
I want to get a project
So that I can monitor the activity of selected repositories

Let's add a test to the existing set of tests ./test/project.js for our feature Get a project. This resource will GET a project from route /project/:id, and return a 200 OK status.

Let's install underscore.js; a utility-belt library that provides functional programming support:

npm install underscore --save
describe('when requesting an available resource /project/:id', function(){
  it('should respond with 200', function(done){
    request(app)
    .get('/project/' + id)
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var proj = JSON.parse(res.text);
      assert.equal(proj._id, id);
      assert(_.has(proj, '_id'));
      assert(_.has(proj, 'name'));
      assert(_.has(proj, 'user'));
      assert(_.has(proj, 'token'));
      assert(_.has(proj, 'created'));
      assert(_.has(proj, 'repositories'));
      done();
    });
  }...

Feature: Edit a project


As a vision user
I want to update a project
So that I can change the repositories I monitor

Let's add a test to our existing set of tests ./test/project.js for our Edit a project feature. This resource will PUT a project to route /project/:id, and return a 204 No Content status:

describe('when updating an existing resource /project/:id', function(){
  var project = {
    name: "new test name"
    , user: login.user  
    , token: login.token
    , repositories    : [ "12345", "9898" ]
  };

  it('should respond with 204', function(done){

    request(app)
    .put('/project/' + id)
    .send(project)
    .expect(204, done);
  });
});

Let's implement the Edit a project feature ./lib/project/index.js and add a put function. We attempt to retrieve a project by calling the static function Project.findOne. If we get an error, we return it; if we cannot find the project, we return null. If we find the project, we update it and return the project:

Project.prototype.put = function...

Feature: Delete a project


As a vision user
I want to delete a project
So that I can remove projects no longer in use

Let's add a test to ./test/project.js for our feature Delete a project. This resource will DELETE a project at route /project/:id and return a 204 No Content status:

describe('when deleting an existing resource /project/:id', function(){
  it('should respond with 204', function(done){
    request(app)
    .del('/project/' + id)
    .expect(204, done);
  });
});

Let's implement the Delete a project feature ./lib/project/index.js and add a del function. We attempt to delete a project by calling the static function Project.findOne. If we get an error, we return it; if we cannot find the project, we return null. If we find the project, we delete it and return an empty response.

Project.prototype.del = function(id, callback){
  var query = {'_id': id};

  ProjectSchema.findOne(query, function(error, project) {
    if (error) return callback(error, null);
    if (project == null) return...

Feature: List projects


As a vision user
I want to see a list of projects
So that I can select a project I want to monitor

Let's add a test to ./test/project.js for our feature List projects. This resource will GET all projects from route /project and return a 200 Ok status.

describe('when requesting resource get all projects', function(){
  it('should respond with 200', function(done){
    request(app)
    .get('/project/?user=' + login.user)
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var proj = _.first(JSON.parse(res.text))
      assert(_.has(proj, '_id'));
      assert(_.has(proj, 'name'));
      assert(_.has(proj, 'user'));
      assert(_.has(proj, 'token'));
      assert(_.has(proj, 'created'));
      assert(_.has(proj, 'repositories'));
      done();
    });
  });
});

Let's implement the List projects feature ./lib/project/index.js and add an all function. We attempt to retrieve all projects by calling the static function Project.find and...

GitHub API


Our project API is complete but things are about to get a little more complicated as we attempt to communicate with the GitHub API. Let's install the following modules.

The github module provides an object-oriented wrapper for the GitHub v3 API; the complete API for this module can be found at http://mikedeboer.github.io/node-github/.

npm install github --save

The async module is a utility module that provides around 20 powerful functions for working with asynchronous JavaScript. The async module is a control-flow module and will allow us to do operations over IO in a clean, controlled way.

npm install async --save

The moment.js is a library for parsing, validating, manipulating, and formatting dates.

npm install moment --save

Feature: List repositories


As a vision user
I want to see a list of all repositories for a GitHub account
So that I can select and monitor repositories for my project

Let's add a test to ./test/github.js for our feature List repositories. This resource will GET all repositories for a project from the route project/:id/repos and return a 200 Ok status:

describe('when requesting an available resource /project/:id/repos', function(){
  it('should respond with 200', function(done){
    this.timeout(5000);
    request(app)
    .get('/project/' + id + '/repos/')
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var repo = _.first(JSON.parse(res.text))
      assert(_.has(repo, 'id'));
      assert(_.has(repo, 'name'));
      assert(_.has(repo, 'description'));
      done();
    });
  });
});

The first thing we need to do is create a GitHubRepo module in ./lib/github/index.js. We start by importing the required modules including github. We define a constructor...

Feature: List commits


As a vision user
I want to see a list of multiple repository commits in real time
So that I can review those commits

Let's add a test to ./test/github.js for our List commits feature. This resource will GET the 10 most recent commits for all repositories in a project via the route project/:id/commits and return a 200 OK status:

describe('when requesting an available resource /project/:id/commits', function(){
  it('should respond with 200', function(done){
    this.timeout(5000);
    request(app)
    .get('/project/' + id + '/commits')
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var commit = _.first(JSON.parse(res.text))
      assert(_.has(commit, 'message'));
      assert(_.has(commit, 'date'));
      assert(_.has(commit, 'login'));
      assert(_.has(commit, 'avatar_url'));
      assert(_.has(commit, 'ago'));
      assert(_.has(commit, 'repository'));
      done();
    });
  });
});

Let's implement the List commits feature...

Feature: List issues


As a vision user
I want to see a list of multiple repository issues in real time
So that I can review and fix issues

Let's add a test to ./test/project.js for our List issues feature. This resource will GET all projects from the route project/:id/issues and return a 200 OK response:

describe('when requesting an available resource /project/:id/issues', function(){
  it('should respond with 200', function(done){
    this.timeout(5000);
    request(app)
    .get('/project/' + id + '/issues')
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var issue = _.first(JSON.parse(res.text))
      assert(_.has(issue, 'title'));
      assert(_.has(issue, 'state'));
      assert(_.has(issue, 'updated_at'));
      assert(_.has(issue, 'login'));
      assert(_.has(issue, 'avatar_url'));
      assert(_.has(issue, 'ago'));
      assert(_.has(issue, 'repository'));
      done();
    });
  });
});

Let's implement the feature List issues and add it to...

Validating parameters with param middleware


You will have noticed that we have repeated the id validation in each of our routes. Let's improve things using app.params.

Here is the offending line of code that simply checks to see if our id is a valid MongoDB id:

if (req.params.id.match(/^[0-9a-fA-F]{24}$/) == null)
  return res.json(400, 'Bad Request');

Let's add a middleware to handle this ./lib/middleware/id.js. We define a validate function that takes four parameters, with the last being the value of id. We then validate the id parameter, returning a 400 Bad Request, if it's invalid. We then call next(), which calls the next middleware in our Express stack:

exports.validate = function(req, res, next, id){
  if (id.match(/^[0-9a-fA-F]{24}$/) == null)
  return res.json(400, 'Bad Request');
  next();
}

Now we can use this id middleware in our Express server. Let's include the param middleware and add this line before the first route so that it applies to all of our routes: ./lib/express/index...

Route improvements


We now have quite a few routes required in our Express server; let's clean this up. A common pattern in node.js is to include an index file that returns all files in its current directory. We will use require-directory to do this for us:

npm install require-directory –save

Let's create a new module ./lib/routes/index.js. with the following code:

var requireDirectory = require('require-directory');
module.exports = requireDirectory(module, __dirname, ignore);

Now, all routes in the ./lib/routes/ folder will be exposed under a single variable, routes:

  var express = require('express')
  , http = require('http')
  , config = require('../configuration')
  , db = require('../db')
  , routes = require('../routes')
  , notFound = require('../middleware/notFound')
  , id = require('../middleware/id')
  , app = express();

app.use(express.bodyParser());
app.set('port', config.get('express:port'));
app.use(express.logger({ immediate: true, format: 'dev' }));
app.param('id', id.validate...

Summary


We have now completed our Web API. We have implemented a basic MongoDB provider; we are using Mongoose to give us a bit of schema support. We have also made a small improvement to our Express server, cleaning up the routes.

In the next chapter, we will consume this API when we build our client.

Left arrow icon Right arrow icon

Key benefits

  • Learn how to build scalable, robust, and reliable web applications with Express using a test-first, feature-driven approach
  • Full of practical tips and real world examples, and delivered in an easy-to-read format
  • Explore and tackle the issues you encounter in commercially developing and deploying an Express application

Description

Building an Express application that is reliable, robust, maintainable, testable, and can scale beyond a single server requires a bit of extra thought and effort. Express applications that need to survive in a production environment will need to reach out to the Node ecosystem and beyond, for support.You will start by laying the foundations of your software development journey, as you drive-out features under test. You will move on quickly to expand on your existing knowledge, learning how to create a web API and a consuming client. You will then introduce a real-time element in your application.Following on from this, you will begin a process of incrementally improving your application as you tackle security, introduce SSL support, and how to handle security vulnerabilities. Next, the book will take you through the process of scaling and then decoupling your application. Finally, you will take a look at various ways you can improve your application's performance and reliability.

Who is this book for?

If you are an experienced JavaScript developer who wants to build highly scalable, real-world applications using Express, this book is ideal for you. This book is an advanced title and assumes that the reader has some experience with Node.js, JavaScript MVC web development frameworks, and has heard of Express before, or is familiar with it. You should also have a basic understanding of Redis and MongoDB.

What you will learn

  • Drive Express development via test
  • Build and consume a RESTful web API using client and server-side templating
  • Secure and protect Express with passport authentication and SSL via stud
  • Scale Express beyond a single server with Redis and Hipache
  • Decouple Express for improved scalability and maintainability
  • Support real-time application development with Socket.IO
  • Handle failures with a minimum impact to service availability using cluster and domains
  • Understand and cope with Express limitations, including when and where to go for help

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 25, 2013
Length: 148 pages
Edition : 1st
Language : English
ISBN-13 : 9781783282500
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 25, 2013
Length: 148 pages
Edition : 1st
Language : English
ISBN-13 : 9781783282500
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 11,470.97
Express Web Application Development
₹4096.99
Advanced Express Web Application Development
₹3276.99
Mastering Node.js
₹4096.99
Total 11,470.97 Stars icon

Table of Contents

7 Chapters
Foundations Chevron down icon Chevron up icon
Building a Web API Chevron down icon Chevron up icon
Templating Chevron down icon Chevron up icon
Real-time Communication Chevron down icon Chevron up icon
Security Chevron down icon Chevron up icon
Scaling Chevron down icon Chevron up icon
Production Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(8 Ratings)
5 star 50%
4 star 12.5%
3 star 37.5%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Gabriela Jack Dec 20, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I was given a copy of this book to review it and I'm pleased because it is a really good book.Although not completely unfamiliar, I am a newbie when it comes to Node.js, Express and related technologies. I have read my fair share of beginners tutorials and books, though, so it wasn't really hard to follow along, plus the book is well structured and the code included with it works without having to debug it or troubleshoot it, which makes life so much easier.There are several things I really liked about this book. First of all, the fact that it doesn't just offer a collection of short snippets and mini-tutorials, but sticks with one complete, real-life like application from end to end, while also teaching you best practices and the correct approach towards designing and structuring your applications. It's really hard to find a book about Express or Node.js that does that. Most of the books and tutorials I've found deal with short and simplified examples of how to build your own chat application and such.Of course, a more realistic example also means that you will find yourself incorporating all sorts of dependencies, libraries and such while working your way through the chapters of this book, because this is what you're likely to find in a real life application. If you, like me, are unfamiliar with some of these libraries, modules and technologies, I recommend that you spend sometime reading about them so you don't just copy and execute the commands included in the book without knowing what you're doing. While the book usually tells you what those libraries and dependencies are, why we need them and how we'll use them in the application, it's really beyond its scope to go into much detail about any of them. It can feel a bit overwhelming for a newbie! That's why I plan on returning to the book and working through all the parts of the example application all over again after I become more comfortable with Grunt, MongoDB and Redis.I was also greatly impressed by the fact that the example application in this book incorporates TDD, specially because that was one of the reasons that first got me interested in learning about Node.js.The only problem I had with this book was that I was never able to access the github repository that the book directs the reader to in chapter one to obtain the source code. I had to get the source code from Packtpub, instead.I would recommend this book to all that want to learn about Node.js and Express beyond the basic tutorials. This is not a tutorial, no, but it is a fine "blueprint" for what a real-life Node.js application looks like.
Amazon Verified review Amazon
Jamie Moon Dec 18, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Recently I started the web service project that transform the legacy APIs and some function to REST open API.In this project, of course, I also had to make the web UI for it and backend system to monitor the usage of each API, user....Although I'm a newbie of Node.js and the way of getting things done, practical examples and experiences were definitelyhelpful.Why did I choose this rating?Node.js is great, but I think people who doesn't have much experiences about Linux system and administration thingscan be confused. this books introduced the things like that with proper depth.Who should I recommend this product to?My project members and everyone who want to know real world node.js examples
Amazon Verified review Amazon
Doug Duncan Dec 20, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Advanced Express Web Application Development by Andrew Keig walks you through building an application that serves as a dashboard for software projects and ties into GitHub to give a single-screen snapshot of the project's issues and commits.The book is well written and easy to follow along with. Being that the author walks you through building a sample application, each chapter logically builds on the previous and steps you through the changes one would make to code as design progresses through the application design.The application is built on Node.js and uses the express framework, but there are numerous other node modules in use including the use of both MongoDB and Redis as data stores. The book is written in a way that even someone pretty new to node should be able to follow along without too many problems.The book progresses through setting up a real simple version of the application and going through iterations where you add in real-time updates and security. It finally ends with how to handle scalability and advice for production deployments.I would definitely recommend this book to anyone who's interested in using node and express for web development.
Amazon Verified review Amazon
David Melamed Dec 15, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had the chance to read an early release of the book and it is just amazing. It is fuelled with all the best practices in the node.js world, the most useful modules everyone should know and gives a great insight about to build a high-scalable node.js based project from scratch.I particularly loved how the sample project - a Github project manager - was structured in a very decoupled and organised way, which allows a clear understanding of the structure as well as more code reuse.Don't be intimated by the "Advanced" adjective! This book should be read by everyone and even newbies in the field will understand most of it.
Amazon Verified review Amazon
Brandon Taylor Jan 28, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
There seem to be some environmental differences from the author's development environment to mine, as I've had to make some adjustments to get testing with grunt and mocha to work as expected.Aside from a couple of errata, this is a well written book that is pretty easy to follow. The author was kind enough to assist me on Github, although we seem to have a difference in opinion on the definition of errata, as you'll see in the comments.In an effort to offer a more constructive review, this has been one of the better books I've purchased for developing with Node. The writing style is easy to follow, without a lot of rambling. I like how the code in the sample application is organized and I think the functionality of the sample project fits very well to modern development contexts.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.