Book Image

Express.js Blueprints

By : Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang
Book Image

Express.js Blueprints

By: Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang

Overview of this book

<p>APIs are at the core of every serious web application. Express.js is the most popular framework for building on top of Node.js, an exciting tool that is easy to use and allows you to build APIs and develop your backend in JavaScript. Express.js Blueprints consists of many well-crafted tutorials that will teach you how to build robust APIs using Express.js.</p> <p>The book covers various different types of applications, each with a diverse set of challenges. You will start with the basics such as hosting static content and user authentication and work your way up to creating real-time, multiplayer online games using a combination of HTTP and Socket.IO. Next, you'll learn the principles of SOA in Node.js and see them used to build a pairing as a service. If that's not enough, we'll build a CRUD backend to post links and upvote with Koa.js!</p>
Table of Contents (14 chapters)

Defining routes


Let's start by defining the first routes we need and how we want them to behave and simple logical steps building what's strictly essential first, in a TDD style.

  1. The first thing is that we need users to be able to register; the smallest test case to register our user is as follows:

    '''javascript
    var dbCleanup = require('./utils/db')
    var expect = require('chai').expect;
    var request = require('supertest');
    var app = require('../src/app');
    
    describe('Registration', function() {
      it("shoots a valid request", function(done){
        var user = {
          'email': 'supertest'+Math.random()+'@example.com',
          'name': 'Super'+Math.random(),
        };
    
        request(app)
          .post('/register')
          .send(user)
          .expect(200, done);
      })
    })
  2. Assuming you have Mocha installed with npm i -g mocha, execute mocha.

  3. See 404? Good start! Now let's expand and create a file, src/route/index.js, which will declare all the routes known to the app. It uses controllers that handle each concern.

  4. Start...