Skip to content

p15martin/node-reference-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This is a reference application for Node.js. It is intended to demonstrate the following principles:

  • test coverage
  • clean code
  • separation of concerns
  • modularity
  • error handling
  • logging
  • timing
  • log correlation

The reference app makes use of the following packages:

  • RequireJS - used for modularity of the codebase and to manage dependencies
  • Connect - provides a middleware framework
  • Connect-JSONRPC - provides JSON-RPC 2.0 support via the Connect middleware framework
  • Mongoose - object modeling tool for MongoDB
  • Async - asynchronous flow control (e.g. performing tasks in sequence or parallel)
  • node-uuid - generation of RFC4122 UUIDS, which is used to generate a unique logid for correlating related activities

The app

The app is intended to be simple. It exposes a basic set of CRUD operations as JSON-RPC services. My database of choice is MongoDB.

Running the tests

Like the professional programmers we are, we will start with the tests. I choose Buster for my test framework and Sinon.JS for my test spies, stubs, and mocks. Sinon.JS is embedded within Buster, which is super nice and makes it super easy to write robust tests.

First off clone the code from GitHub.

Next, make sure you are at the command prompt and are in the directory that contains the code for the reference app.

Install the package dependencies:

npm install

Install Buster:

sudo npm install –g buster 

(if you have problems installing Buster then please refer to this page)

Run the tests:

buster test

Easy! The convenience of the Buster test runner makes it ideal for integration with a CI server such as Jenkins. It also has a headless test runner, which is perfect for your client-side tests.

Deploy to Heroku

I developed the reference app against Heroku. For my MongoDB database I used the Heroku add-on for MongoLab.

If you don't already have an account on Heroku then create one and install the command line client (CLI).

Again, make sure you are at the commond prompt, and are in the directory that contains the code for the reference app.

Create an app on Heroku using the Cedar stack:

heroku create --stack cedar

When your app has been created you should see the app name e.g. cold-stone-7259.

Add your Heroku app as a remote repository (remember to use your app name):

git remote add heroku [email protected]:cold-stone-7259.git

Add the MongoLab add-on:

heroku addons:add mongolab:starter

Deploy the app (using a git push):

git push heroku master

If you look at Heroku logs you should see that the app has started:

heroku logs -s app

Use the app

We will continue to work at the command prompt. If you want open a second terminal window, again from the directory that contains code for the reference app, tail the logs so you can see what is going on:

heroku logs -s app --tail

I am using cURL to hit the RPC services. As before, remember to use your app name in the URL.

Add a contact:

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "addContact", "params": ["Peter", "Martin", 123678924], "id":2 }' -i http://cold-stone-7259.herokuapp.com

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "addContact", "params": ["Joe", "Bloggs", 789678068], "id":2 }' -i http://cold-stone-7259.herokuapp.com

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "addContact", "params": ["Fred", "Bloggs", 789461207], "id":2 }' -i http://cold-stone-7259.herokuapp.com

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "addContact", "params": ["John", "Doe", 123563846], "id":2 }' -i http://cold-stone-7259.herokuapp.com

Find all contacts by last name:

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "findAllContactsByLastName", "params": ["Martin"], "id":2 }' -i http://cold-stone-7259.herokuapp.com

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "findAllContactsByLastName", "params": ["Bloggs"], "id":2 }' -i http://cold-stone-7259.herokuapp.com

Update the cell number for a contact:

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "updateCellNumber", "params": ["4f1a0dc1458dbb0100000001", 111222333], "id":2 }' -i  http://cold-stone-7259.herokuapp.com

Delete a contact:

curl -H "Content-Type: application/json" -d '{ "jsonrpc": "2.0", "method": "deleteContact", "params": ["4f1a0dc1458dbb0100000001"], "id":2 }' -i http://cold-stone-7259.herokuapp.com

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published