Example Hapi-backed API Server with testing, CI, and Swagger documentation generator.
Updated and tested with latest hapi packages as of 8/4/2016.
Requires Node v5.2.0+
npm install #install dependencies
npm start # start server
npm test # run tests-
Notable npms
- hapi, joi
hapiis a popular web/services nodejs framework. It is strict about routing and validation out of the box and has a mature plugin/extension system. I've usedexpressfor a long time, but since tryinghapi, I haven't really looked back. On the surface the two frameworks look similar. In my experience though, I found thathapimanaged to scale much better with increasing complexity and made it easier to test, debug, and write better code.joiis an awesome schema/object definition and validation library. This project uses it enfoce API input/output validation and generate documentation.
- hapi-swagger
- Swagger is an API framework and standard.
hapi-swaggeris a hapi plugin that generates awesome interactive API documentation & UI right from our code API definitions. I gotta say... it's really nice to keep everything in one place.
- Swagger is an API framework and standard.
- lab
labis hapi's version of mocha. It's a test runner, nicely packaged with a linter and code-coverage reporter. Nothing you wouldn't expect here. (Unless you've never written tests)
- hapi, joi
-
Testing
- Unit Tests (
/test/unit/*)- Functions containing business logic are defined in isolation from the framework making them testable without a running server
- Functional Tests (
test/functional/*)- These tests are meant to target the API endpoints, covering functionality end-to-end.
- We should try to write these tests in a way that they become easily exportable to run automatically via tools like New Relic Synthetics
- ESLint
labalso includes a linter (eslint by default), which is executed when tests run. The default configuration can be customized via the.eslintrc.jsonfile.
- Code Coverage
labanalyzes the code and returns the code coverage ratio when running the test. It also points out which lines of code are missing coverage. A nice reminder to write tests for any newly added functionality.
- Unit Tests (
-
Documentation
hapi-swaggeris configured inapp.jsand generates a very nice html page with an interactive Swagger compatible API.- Once your server is running locally, visit http://localhost:3000 to check out the docs.
- My typical workflow is to write the documentation first (by setting up the hapi routing #2BirdsWith1Stone), then to write the functional tests, then a combination of code and unit tests ala TDD until I'm satisfied with the results.
-
CI
- This repo also integrates TravisCI, which runs the tests defined above on every pull request, blocking a merge if the test does not pass. Not very useful for a one person project, but crucial when a team of developers is involved.
- add stubbing framework to imitate real external service calls.
- Figure out an easy way to test multiple hapi services together, in a microservice environment.
- Integrate
boomfor API error responses.