Awesome
API Proxy 🚀
Simple API Proxy project - no storage layer, proxies results from underlaying API, built to be a boilerplate for API's, good training-hack-session and memory refresher.
Tested with NHTSA NCAP 5 Star Safety Ratings API.
Up and running
Local development environment
You have two options in doing so, running directly on the local terminal, or using docker along with docker-compose
-
Terminal: Copy/rename
.env.example
file to.env
set any changes to theENV
variables and then runnpm install && npm start
oryarn && yarn start
-
Docker (Compose): Modify the
docker-compose.yml
file, set any changes on the files and then simply rundocker-compose up dev
Production deployment
- Docker (with Compose): Modify the
docker-compose.yml
file, set any changes on the files and then simply rundocker-compose up -d prod
. Also, can be piped through a CI (such as CircleCI) and be deployed via Google Cloud SDK To its Kubernetes Engine for example.
Swagger
This project implements swagger-ui-express
along with swagger-jsdoc
which easily deploys a swagger UI page with all relevant documentation that is previously set in JSDoc format, following the OpenAPI a.k.a. Swagger specification.
After getting the API server up and running, you can navigate to http://localhost:8888/api-docs to use the Swagger UI :rocket:
Notes
- Handles malformed JSON when parsing the body, it returns 200 and an empty
baseModel
object, in case the test suite you are using doesn't deal with status codes, the proper way to handle any error (such as missing params in the JSON body) would be returning the corresponding status code (400 Bad Request) and relaying any messages; or returning 200, thebaseModel
and a meaningful message, or even an error object with error codes such as:
{
// Spread the baseModel[Count, Results, Message] Object
...baseModel
"error":{
"code": -999,
"message": "Something really bad happened"
}
}
Addendum
ENV variables
NODE_ENV
: determines the environment that the API is running, possible values:development
production
API_PORT
: the port that the API will be exposedAPI_NAME
: Name of the APIAPI_VERSION
: SemVer of the API (i.e: 1.0.0)API_DESCRIPTION
: Description of the APIAPI_RESPONSE_VERBOSE
: Allows to pipe he messages of the underlaying API to the response objectVEHICLE_API_BASE_URL
: The base of the API URL for the VEHICLES example
Node version
The project uses node 8.11 the latest LTS as of June 2018
Packages used
- Express - minimalist and flexible framework with plenty of middleware options to extend
- Babel - javascript transpiler of usage of es6, es7 features (object/array spread operators, await/async)
- dotenv - zero-dependency module that loads environment variables from a .env file into process.env
- helmet - helps on securing (a litle bit) the node.js app by setting a series of headers
- morgan - HTTP request logger, to see what's being sent to the API
- swagger-jsdoc - Generates swagger doc based on JSDoc, used with together with
swagger-ui-express
- swagger-ui-express - Middleware for the express app to serve the Swagger UI bound to the
swagger-jsdoc
definitions. - nodemon [
dev-dep
]- monitors changes in the node.js app and automatically restarts the server - eslint [
dev-dep
]- linting for javascript, keeps code files in shape