Giter VIP home page Giter VIP logo

hapi-starter-kit's Introduction

Hapi Starter Kit | Hapi based REST application boilerplate, uses async/await

Build Status Code Climate Test Coverage bitHound Overall Score bitHound Dependencies MIT License PRs Welcome

Overview

A lean boilerplate application for building RESTful APIs (Microservice) in Node.js using hapi.js. Follows industry standard best practices, and uses latest async/await ES8 feature. Bring your own front-end. Plug-in your own Database.

Features

Feature Summary
Uses latest ES8/ES2017 features (async/await) Uses latest ES7 and ES8 features including async/await
Application Configuration via config Node-config organizes hierarchical configurations for your app deployments. It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).
Auto Documentation via hapi-swagger This will self document the API interface. You can also tests your APIs using the Swagger interface.
Unit and Integration Tests via Mocha with async/await Demo unit and integration tests using latest ES7/ES8 features.
Code Coverage via istanbul Supports code coverage of ES8 code using istanbul and mocha. Code coverage reports are saved in coverage/ directory post yarn test execution. Open coverage/lcov-report/index.html to view coverage report. yarn test also displays code coverage summary on console. Code coverage can also be enforced overall and per file as well, configured via .istanbul.yml
Logging via bunyan Bunyan is simple and fast JSON logging library. Logs timestamp, machine name and process ID and most importantly makes it really easy to parse logs as it logs in JSON format. You can selectively turn on/off logs by setting log level via LOG_LEVEL env variable.
Code Linting via Standard JavaScript code linting is done using ESLint - a pluggable linter tool for identifying and reporting on patterns in JavaScript.
API parameter validation via joi Validate body, params, query, headers and cookies of a request (via middleware) and return a response with errors; if any of the configured validation rules fail. You won't anymore need to make your route handler dirty with such validations.
Application monitoring via New Relic Set NEW_RELIC_APP_NAME and NEW_RELIC_LICENSE_KEY environment variables in production environment to get metrics in New Relic Dashboard
Pre-commit hooks via husky Runs lint and tests before any commit is made locally, making sure that only tested and quality code goes through
Uses Sinon for spies, stubs and mocks Standalone and test framework agnostic JavaScript test spies, stubs and mocks
Uses nock for mocking http requests in integration tests Nock is an HTTP mocking and expectations library. It can be used to test modules that perform HTTP requests in isolation.
Uses yarn over npm Uses new released yarn package manager by facebook. You can read more about it here
Docker compatible Docker and Docker Compose files to develop and deploy via Docker
Uses EditorConfig EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs

Requirements

  • node.js >= 8.4.0
  • yarn >= 0.27.5
  • docker
    • Docker is optional and is required only if you want to develop and deploy using Docker

Getting Started

# Install dependencies
$ yarn

yarn

# Start Server
# Set environment variables defined in `config/custom-environment-variables.json` like `OPEN_WEATHER_API_KEY=xxx`
$ yarn start

yarn start

# Try GET /ping to make sure server is up
$ curl http://localhost:3030/ping
# Run Tests
$ yarn test

yarn test

Docker

Development

# copy sample.dev.env to dev.env
$ cp bin/sample.dev.env bin/dev.env
# Start Server
$ bash bin/development.sh

Docker Development

Tests

# copy sample.test.env to test.env
$ cp bin/sample.test.env bin/test.env
# Run Tests
$ bash bin/test.sh

docker test

Environment Configuration

config is used to configure application.

  • Default values of environment variables, which are common across all environments can be configured via config/default.json
  • Values specific to a particular environment can be set by creating a file with same name in config directory. Like config/test/json for test environment.
  • config/custom-environment-variables is used to read values from environment variables. For ex. if APP_PORT env var is set it can be accessed as config.get('app.port'). You can read more on custom environment variables here.

More Tasks

# Run lint
yarn lint

yarn lint

Deployment

  • Simply set environment variables defined in bin/sample.dev.env in your own environment (AWS, Heroku etc) and yarn start

Docker

  • Build the docker image
    • docker build -t hapi-starter-kit-oss .
  • Start Docker Container
    • docker run -d -p 3030:3030 --name hapi-starter-kit-oss hapi-starter-kit-oss

Documentation

  • hapi-swagger self documents all the APIs.
  • Visit http://localhost:3030/documentation to access the documentation after starting the server. Documentation

Miscellaneous

  • To turn off logs getting logged via good-console in development environment, remove it from plugins.js

Issues

Please feel free to open an issue if you can have any questions or trouble using this starter kit.

Contributions

Contributions are all welcome and encouraged. For code contributions submit a pull request with unit test.

License

This project is licensed under the MIT License

hapi-starter-kit's People

Contributors

deepak365 avatar kunalkapadia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hapi-starter-kit's Issues

Suggestion: add import/export support with esm

First, thank you for hapi-starter-kit. I have tried at least half a dozen different npm packages that all claim they create your swagger for you but they all came up short of that promise until I found hapi-starter-kit. This is the only true, out of the box, node/swagger solution I've found so thank you again.

One thing I did in my hapi-starter after I got it up and running, was to add support for import/export using esm. It was pretty much seamless. I moved the contents of the root index.js into a new file called main.js and then changed index.js to this:

// eslint-disable-next-line no-global-assign
require = require('esm')(module)
module.exports = require('./main.js')

And that's all there was to it. Just thought I'd offer it up as a suggestion since hapi-starter-kit is ES8/ES2017 friendly.

Need help to run yarn test on Windows 7

When I tried 'yarn test' first time, I got error:

$ NODE_ENV=test istanbul --include-all-sources cover _mocha -- -u bdd $(find test -name '*.js') && istanbul check-coverage && echo All tests passed �
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.

I then copied the 'test' script and create a test:win script:

"test:win": "SET NODE_ENV=test istanbul --include-all-sources cover _mocha -- -u bdd $(find test -name '*.js') && istanbul check-coverage && echo All tests passed 👍",

I ran 'yarn test:win', I got the istanbul's error:

yarn test:win
yarn run v1.2.1
$ SET NODE_ENV=test istanbul --include-all-sources cover _mocha -- -u bdd $(find test -name '*.js') && istanbul check-coverage && echo All tests passed �
ERROR: No coverage files found.
Try "istanbul help" for usage

Do I need to manually create a coverage file or am I missing any dependencies?

Thanks

How to add rethinkdb or other database

Hi,
I am trying to figure out the best way to add rethinkdb to this boiler plate. I am using hapi-rethinkdb which binds rethinkdb and connection to the server context in a request per this documentation.

https://github.com/ghostbar/hapi-rethinkdb#use-plugin

But in this boilerplate this context is not available in the handlers. So how do I setup the handlers on this starter kit so that this.rethinkdb is available in the handler?

I am having to instead use var r = request.server.plugins['hapi-rethinkdb'].rethinkdb to get a connection to the rethink db in every time in each handler.

thanks,

hapi 17

Hi, do you have an updated version for Hapi 17.1.1 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.