This project has initials configurations necessary to create a Rest API in NodeJs with several pre-configured features. In addition, it has an architecture that reduces the writed code in the creation of new routes and validations. See the features below.
For create a new module you just need to specify a Mongoose model and inherit the crud functions from the BaseController. If you need, is possible override the pre-defined functions. See the TemplateController to better understand.
Validate your requests on API with express-validator. It's possible create Validator Schemas or use Mongoose schemas to generate express-validator schemas.
For manager MongoDB connection e operations, use Mongoose and integrate Mongoose Schema to express-validator to validate requests on API.
Translate API response massages using i18n lib. You just need to set the locale in the request headers.
Run the application using Docker. Below are the commands needed to run the app on Docker:
On root path of project:
# Create docker image
docker build -f ./dockers/Dockerfile -t node-rest-api:latest .
# Run docker image in localhost network
docker run -d --env-file ./config/local.env --network=host --name node-rest-api node-rest-api:latest
# Check image is running
docker ps
For manager logs we use winston. Is possible integration with LogStash and others applications.
Improve code quality and patterns using eslint and prettier.
Improve unit and integration tests using jest pre setup and structure with supertest.
For automate testing, we use Postman to make requests to our APIs.
To create JWT certificates use that commands:
openssl genpkey -algorithm RSA -out rsa_private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem
After creates the certificates, put the files content on .env file in JWT_PRIVATE_KEY and JWT_PUBLIC_KEY variables.
To use ACL you must set the Enviroment variable USE_ACL=true. With this variable we enable ACL for all resoures in our API.
In src/seed/profiles.js your must include new resouces on three defaults profiles: Admin, User, Guest. That profiles will be created on App starts. You can customize existing profiles names or create new ones.
"n": No access to resource
"r": Access to read the resource (GET, OPTIONS, HEAD)
"w": Access to read and write the resource (GET, OPTIONS, HEAD, POST, PUT, PATCH)
"m": Access to manager (read, write and delete) the resource (GET, OPTIONS, HEAD, POST, PUT, PATCH, DELETE)
{
"_id": 1,
"name": "Admin",
"acl": {
"profiles": "n", // Disable all operations for profiles endpoint (api/profiles)
"users": "r", // Enable read operations for users endpoint (api/users)
"produtcs": "w", // Enable read and write operations for produtcs endpoint (api/produtcs)
"reports": "m" // Enable read, write and delete operations for reports endpoint (api/reports)
}
}