Install or Update existing node to Node.JS LTS version
Clone this repo
cd /path/where/your/cloned/the/repo
npm install
to install all required dependencies
Install MongoDB Community Edition (instructions) and run it by executing mongod -d /path/where/you/want/to/store/the/data
gulp start
Start the server
gulp quality
Run code quality
gulp test
Run tests
gulp e2e
Run test automation
The API is available at http://localhost:8080
mongoose-unique-validator
plugin helps us by formatting the error like a normal mongoose ValidationError
.mongod --dbpath ~/mongodb/data/db/
/usr/local/var/mongodb/mongo
lib/index2.js
- The entry point to our application. This file bootstrap our HapiJS server. It also requires the routes and models we'll be using in the application.lib/config/
- This folder contains plugins configuration as well as a central location for configuration/environment variables.lib/modules/api/*
- This folder contains the routes definitions for our API.Requests are authenticated using the Authorization
header with a valid JWT. We use one hapijs pluggin in lib/modules/auth/index2.js
that can be used to authenticate requests. The hapi-auth-jwt2
plugin using our application's secret and will return a 401 status code if the request cannot be authenticated. The payload of the JWT can then be accessed from request.auth.credentials.user
in the endpoint.
HapiJS auth mecanism provide 3 Authentications mode:
{ auth: 'jwt' }
set authentication to required.{ auth: { mode: 'optional', strategy: 'jwt' } }
config options in the same way as { auth: 'jwt' }
, but will not return a 401 status code if the request cannot be authenticated (JWT must be valid).{ auth: 'try', strategy: 'jwt' }
similar to optional
but invalid JWT will pass with request.auth.isAuthenticated
set to false
and request.auth.credentials
set to null
.HapiJS use Boom for errors response that use a particular format response, so we need to reformat it, to meet the Backend API specs errors handling section. So we added a preResponse
server extension, to reformat it in lib/modules/api/index2.js
.
Joi is used for validating request params/payload and response payload.
We use hapi-swagger for the API endpoints documentation. Documentation available at http://localhost:8080
.