quick and dirty api boilerplate
|
|
4 jaren geleden | |
|---|---|---|
| couchdb | 4 jaren geleden | |
| src | 4 jaren geleden | |
| .env.example | 4 jaren geleden | |
| .eslintrc.cjs | 4 jaren geleden | |
| .gitignore | 4 jaren geleden | |
| .nvmrc | 4 jaren geleden | |
| .prettierrc | 4 jaren geleden | |
| README.md | 4 jaren geleden | |
| docker-compose.yml | 4 jaren geleden | |
| package-lock.json | 4 jaren geleden | |
| package.json | 4 jaren geleden |
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 npm modules, not a lot of code, and most things are simply factories with dependencies passed in.
Import start from easy-api.js, call like so:
start({
routes[],
host,
port,
middlewares[],
couchDbOpts{},
restifyOpts{}
})
The included example uses docker and docker-compose, so the db host is set to
"couchdb" and the restify server listens on 0.0.0.0:8080. You'll need to rename
.env.example to .env and change the password.
Objects are stored in couchdb 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 an array of wrapped dbs, yup and log instance as
an object ({dbs, yup, log}) and expected to return:
get/api/hello/:nameIf a route passes a schema, the request will be validated against it before any other handlers are called.
req.paramsreq.bodyWhen validation fails, a HTTP 400 is returned and the remaining handlers are not processed.
Middlewares are defined as factories, returning a restify-compatible middleware. They are passed the server instance and restify, like so:
const middlewareToBeApplied = middlewareFactory({server, restify});
server.use(middlewareToBeApplied);