Hapi, API boilerplate using tokens
This web API was built to demonstrate some of the hapi features and functionality. I often get asked
how to combine API user logon with token access and swagger documentation. The best answer I have found is to use
a very small amount of customisation for the swagger UI, this can easily be done with hapi-swagger
plugin.
Online Demo: https://hapi-token-docs.herokuapp.com/
API has:
- Github based signin
- Cookie session for API users
- Token based access control to parts of the API
- Auto documentation with swagger UI
- Customised swagger layout
- Token automatically add to user request through swagger
- Redirects to https
The API is a simple calculator that allows you to add, subtract, divide or multiple two numbers without logging on. To demonstrate a more common set of API calls I also added methods to store sums into a mongodb database to do this you need to loggon and get a token.
Learn more about the JSON Web Tokens at https://jwt.io/
You need have git, node.js and mongodb install on your computer and github account
- From a commandline run
$ git clone https://github.com/glennjones/hapi-token-docs.git
- Move into the project directory
$ cd hapi-token-docs
- Logon to Github and go to https://github.com/settings/developers
- Click the "Register new application" use "http://localhost:3033" two url fields
- Once created make a note of the "Client ID" and "Client Secret"
- Create a new file called
.env
this will store our environment variables - Within the
.env
file add the following lines text changing the values
PRIVATEKEY=some-text
COOKIE_PASSWORD=some-text-more-than-32-chars
GITHUB_PASSWORD=some-text-more-than-32-chars
GITHUB_CLIENTID=client-id-you-just-got-from-github
GITHUB_CLIENTSECRET=client-secret-you-just-got-from-github
ISSUCURE=false
- Run
$ npm install
- Start the mongodb server
$ mongod
- Run
$ node app
- Connect to the server using
http://localhost:3033
The project has a few unit tests. To run the test within the project type one of the following commands.
$ lab
$ lab -r html -o coverage.html
$ lab -r html -o coverage.html --lint
$ lab -r console -o stdout -r html -o coverage.html --lint
If you are considering sending a pull request please add tests for the functionality you add or change.
If you find any issue please file here on github and I will try and fix them.