quick and dirty api boilerplate
|
|
vor 4 Jahren | |
|---|---|---|
| src | vor 4 Jahren | |
| .env.example | vor 4 Jahren | |
| .eslintrc.cjs | vor 4 Jahren | |
| .gitignore | vor 4 Jahren | |
| .nvmrc | vor 4 Jahren | |
| .prettierrc | vor 4 Jahren | |
| README.md | vor 4 Jahren | |
| docker-compose.yml | vor 4 Jahren | |
| package-lock.json | vor 4 Jahren | |
| package.json | vor 4 Jahren |
Quick and dirty API boilerplate, using:
There are many limitations to this configuration, it is intended as a debugging/prototyping tool.
There are fairly few dependencies and most things are simply factories with instances passed in.
Import start from easy-api.js, call like so:
start({
routes[],
host,
port,
redisOpts{},
restifyOpts{}
})
The included example uses docker and docker-compose, so the db host is set to "redis" and the restify server listens on 0.0.0.0. You'll need to rename .env.example to .env and change the password.
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"]).
There is no hardening or security intention behind this - it's simply a fast hash to use as an ID.
Routes are defined as simple factories, passed a db wrapper, yup and log instance as an object ({db, yup, log}) and expected to return:
get/api/hello/:nameIf a route passes a schema, the request will be validated against it:
req.paramsreq.bodyWhen validation fails, a HTTP 400 is returned and the handler is not processed.