Tube Server

This is a Hapi server, backed by MongoDb & Redis for the React-Native mobile app Shuffle.

####You can consider this a "starter server" or a "boilerplate" or maybe just an "example" of how these technologies work together. In any case, I hope there's something here you can learn and apply to your project!

Services Javascript noSQL

hapi | mongodb | | jmeter | swagger | redis| | blazeMeter| nodeMailer | mongoose |

Join the chat at https://gitter.im/bartonhammond/snowflake License

Content

Usecase

Summary

Hapi

The nodeJS server uses Hapi. Hapi was developed and Open Sourced by Walmart Labs. It has been battle tested by Walmart, the largest retailer on earth. I chose it over Express 'cause Hapi is more targeted to API support and it looked interesting.

This server is documented here in its entirety.

Here's some flavor of what Hapi offers. Below is the declarative definition of the /account/login end point. The payload is validated here and shows how the username has a regex expression and is required. The same for the email. The config option has the tags, description, notes that document how the api is used. The handler is defined elsewhere. Separating the end point validation and declaration from the implementation cleans up the code.

  {
    method: 'POST',
    path: '/account/login',
    handler: AccountHandlers.loginUser,
    config: {
      // Include this API in swagger documentation
      tags: ['api'],
      description: 'A user can login',
      notes: 'The user login will return a sessionToken',
      validate: {
    payload: {
          //username required with same regex as client          
      username: Joi.string().regex(CONFIG.validation.username).required(),
          //password required with same regex as client
      password: Joi.string().regex(CONFIG.validation.password).required()
    }
      }
    }
  },

MongoDb

Mongodb will host our documents, namely User information, at this time. We'll be using Mongoose for interacting with Mongo within our code.

Once you're ssh'd into Openshift via rhc ssh -a mysnowflake, you can use the mongo shell.

Redis

Redis is fantastic for key,value pair access. We're using it here for "Black Listing Json Web Tokens". You can read about this concept here https://auth0.com/blog/2015/03/10/blacklist-json-web-token-api-keys/

Swagger

Swagger provides the api documentation - simply augmenting the endpoints generates a page showing all the API access points.

Shown below is the generated API documentation -

Swagger docs

NOTE: you can test the APIs directly from the browser with the forms that Swagger provides!

Swagger form

Jason Web Token

JWT is used in the Authentication as a Session Token. You can read the docs here showing how it's setup.

JMeter

Using JMeter allowed me to performance test the API. I created a test suite with JMeter as shown below and debugged the script by running locally. Once I was satisfied, I changed the HTTP Request Defaults and uploaded to BlazeMeter for testing.

Shown below is the script defined in JMeter

JMeter setup

BlazeMeter

BlazeMeter was used to perform the tests as it is much better equipped to host the threads then my personal mac.

Running BlazeMeter

The following screens show the results of running 50 concurrent users performing the following actions with a 1 second delay between each action:

Original Test Configuration Original Test Configuration

Overview Overview

Timeline Report Timeline Report

Load Report Load Report

Aggregate Report Aggregate Report

Monitoring Report Monitoring Report


Setup

Below are the instructions for setting up the server on your local machine. These instructions work fine on the Mac - no promise is made for other OSs.

You may need to "Allow Less Secure Apps" in your gmail account (it's all the way at the bottom). You also may need to "Allow access to your Google account"

Locally (one time only setup)


cd redis-2.8.24
make
cd src/
./redis-server
npm start