Skip to content

juliancoleman/js-hapi-api

Repository files navigation

JS Hapi API

Build Status

Code Climate Issue Count Test Coverage

Codacy Badge

The js-hapi-api is the most standardized Hapi API to date. While this repo is a bit young, this project and all its efforts are well over two years old, based on an old concept by @jadengore for a project for a public curation of recipes (without the typical suck of your average ad-filled application). Out of the box, this API sports endpoints for users CRUD, as well as user authentication using JWTs.

Setup

yarn

I use the yarn package manager. You can download it in the link provided if need be. Otherwise, just cd into where you've cloned this repo and run yarn from your favorite Terminal emulator.

.env

Your .env file needs the following content

DATABASE_POSTGRESQL_USERNAME=your_database_username # change if not using Postgres
DATABASE_POSTGRESQL_PASSWORD=your_database_password # change if not using Postgres
DATABASE_NAME=your_database_name
DATABASE_HOST=your_database_host # typically 127.0.0.1
JWT_KEY=your_jwt_secret_key

PostgreSQL

For this API, I've gone with PostgreSQL, but you can use any SQL database interchangeably. Just be sure to add your DB with yarn, change the identifier in config/Knexfile.js, and change the POSTGRESQL to your database flavour as mentioned above in the .env section.

To download ProstgreSQL on MacOS, use Homebrew, don't bother using Windows, and for Linux (Debian, or Ubuntu), I personally recommend downloading from the PostgreSQL Github Repository. Follow the instructions on how to build it. If you're ever unsure on how to do this, "seek first His Kingdom" and then ask someone. What I mean by this is really consider what it means to be an engineer.

From here, I would recommend following the instructions from DigitalOcean on how to set up Postgres.

Knex

Now that your .env and PostgreSQL client have been set up, hop into Terminal and run the following

yarn knex migrate:latest

Back to the .env

Just make sure your .env file reflects your Postgres setup.

Docs

All the docs for the present functionality are contained within the wiki. I would recommend reading up on these to get an idea on how to interact with the API, and example payloads.

Testing

As you've noticed above, I use Semaphore CI. If the badge ever says fail, chances are I'm working on it that second. But if you'd like to run it locally, chances are you've run yarn from the Terminal.

You can run the testing suite by running

yarn test

If any of the tests should fail, please make sure your .env and database are set up properly.

Important Docs

HapiJS - A rich framework for building applications and services

hapi-auth-jwt2 - Secure Hapi.js authentication plugin using JSON Web Tokens (JWT) in Headers, Query or Cookies

Joi - Object schema description language and validator for JavaScript objects

BookshelfJS - Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder

bookshelf-bcrypt - Automatic password hashing for your bookshelf models

bookshelf-json-columns* - Parse JSON columns with Bookshelf.js

bookshelf-paranoia - A bookshelf plugin to soft-delete data

KnexJS - Knex.js is a "batteries included" SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, and Oracle

Bluebird - Bluebird is a fully featured promise library with focus on innovative features and performance

jsonwebtokens - JWTs are an open, industry standard RFC 7519 method for representing claims securely between two parties

Ramda - A practical functional library for JavaScript programmers

Mocha - Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser

Chai - Chai is a BDD / TDD assertion library for node and the browser

Nodemon - Monitor for any changes in your node.js application and automatically restart the server

* not included, but available if required

Final comments

Now that wasn't too bad of a read, was it? By now, you're probably wondering how the heck to actually use this API as boilerplate code. From here, I'll go ahead and point you to the repo wiki. The JS Hapi API was designed to have an incredibly simple setup. Extending the application is a courageous endeavor. Godspeed, homie.

Releases

No releases published

Packages

No packages published