quick and dirty api boilerplate

Craig Fletcher 8de3d7b176 Add logging преди 4 години
src 8de3d7b176 Add logging преди 4 години
.eslintrc.cjs 4337b08561 Initial commit преди 4 години
.gitignore 8de3d7b176 Add logging преди 4 години
.nvmrc 4337b08561 Initial commit преди 4 години
.prettierrc 4337b08561 Initial commit преди 4 години
README.md 8de3d7b176 Add logging преди 4 години
package-lock.json 8de3d7b176 Add logging преди 4 години
package.json 8de3d7b176 Add logging преди 4 години

README.md

Easy API boilerplate

Quick and dirty API boilerplate, using:

  • Redis for the DB
  • Yup for validation
  • Restify for the web server/routing
  • Winston for logging

There are many limitations to this configuration, it is intended as a debugging/prototyping tool.

Usage

Import start from easy-api.js, call like so:

start(
  routes[], 
  host = '127.0.0.1', 
  port = 8080, 
  dbOpts{}, 
  serverOpts{}
)

Storage

Objects are stored in redis by calculating the sha256 hash, hex digest from a "path" array for convenience (e.g. ["users", req.params.userName, "stats", "visits"]).

Routes

Routes are defined as simple factories, passed a db wrapper, yup and log instance as an object ({db, yup, log}) and expected to return:

  • type: HTTP verb, e.g. get
  • route: restify path string, e.g. /api/hello/:name
  • handler: restify handler function for route
  • schema (optional): yup schema to validate against

Validation

If a route passes a schema, the request will be validated against it:

  • get requests validate against req.params
  • post requests validate against req.data