A stripped down API boilerplate built around NodeJS and OpenAPI (Swagger) V3.
This code is quite old now and does not support the very latest versions of Express.
There's not much need for it any longer so I am archiving it.
Set the following environment variables
| Variable | Default | Notes |
|---|---|---|
PORT |
8282 |
The port the server listens on |
If it's running locally (see below) point a browser at any of the following routes:
- localhost:8282
- localhost:8282/ping
- localhost:8282/api/v1/hello/some name
- localhost:8282/api/v1/snooze
and see the API docs at
I used to write a lot of APIs and distilled my then best-practice into this codebase to save myself time, and as example code for other developers wondering how to do this sort of stuff.
This is a GitHub Template so either click the 'use this template' button above, or clone this rep, or fork it, nuke the existing .git data and replace with your own git init and git flow init edit the README.md file, add your own details to api.yml and add routes to src/api/ as you like.
-
a couple of root-level API routes and simple controllers
src/api/pingsrc/api/versions
-
a versioned API route and controller
src/api/v1/hello
-
example of an asynchronous route using
route-asyncsrc/api/v1/snooze
-
automatic construction of api controller routes using
traverse-folders -
automatic linking of swagger paths to controllers using
swagger-routes-express -
request and response validation using
express-openapi-validator -
automatic generation of API docs using
swagger-ui-express -
simple logging (swap out the code in
src/utils/loggerto add your own) -
standardised
node-http-errorandhttp-status-codesand simplegenericandnotFounderror handlers -
dotenvsupport -
the swagger editor as an easy to invoke docker image
-
simple
project.tomlbuildpacks config.
-
unit testing using
mocha,sinon,chai, andproxyquire
-
requestandresponsemocks usingmock-req-res -
100% unit test coverage using
nyc -
integration testing using
supertest -
circleciintegration -
snykintegration
I've paired this right back to the simplest, most generic API I could, so there's no
- authentication (add
passportand updatesrc/utils/makeAppand add appropriate security blocks toapi.yml) - rate limiting
- middleware (roll your own and update
src/utils/makeApp) - monitoring
- sockets or event listeners
- databases, search systems, etc
| Branch | Tests | Code Coverage | Audit | Comments |
|---|---|---|---|---|
develop |
Work in progress | |||
main |
Latest Production Release |
- NodeJS, I use
nvmto manage Node versions —brew install nvm. - Docker if you want to use the Swagger Editor, or you wish to use a
buildpack. Use Docker for Mac, not thehomebrewversion. - Pack to use
buildpacks—brew install buildpacks/tap/pack
Clone this (or better yet, fork it then clone your fork)
npm install
npm startYou can then go to localhost:8282/docs to see the docs.
You can put environment variables in a .env file.
You can use an heroku buildpack as follows:
pack build api-server-boilerplate --builder heroku/buildpacks:18
docker run api-server-boilerplateOr tweak the project.toml file to use whatever buildpacks and environment variables you wish.
| Service | Port | Command | Notes |
|---|---|---|---|
| Swagger Editor | 8080 |
npm run swagger |
The swagger editor |
Copy and paste the api.yml file into the editor to edit it.
npm testto run the unit testsnpm run test:serverwill run the integration testsnpm run lintwill lint itnpm run prettierwill prettify itnpm run test:unit:covwill run the unit tests with code coverage
Please see the contributing notes.