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.
'''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); }) })
npm i -g mocha
, execute mocha
.src/route/index.js
, which will declare all the routes known to the app. It uses controllers that handle each concern.user.js
, which implements a create action, as shown in the following code:'''javascript // src/routes/index.js module.exports = function() { var router = require('express').Router(); var register = require('./user)(); router.post("/user", user.create); return router; }; // src/routes/user.js module.exports = function() { var methods = {}; methods.create = function(req,res,next) { res.send({}); } return methods; };
routes/index.js
.At this stage, we know that the testing setup works. Next, let's move onto persistence and some business logic!