Skip to content

elitonfreitas/node-api-rest-boilerplate

Repository files navigation

node-api-rest-boilerplate

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.

Unit Test Coverage

Sonar Quality Info

Bugs Quality Gate Status Security Rating Duplicated Lines (%) Maintainability Rating

Features

Create new modules easily

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.

Express + express-validator

Validate your requests on API with express-validator. It's possible create Validator Schemas or use Mongoose schemas to generate express-validator schemas.

Mongoose

For manager MongoDB connection e operations, use Mongoose and integrate Mongoose Schema to express-validator to validate requests on API.

i18n

Translate API response massages using i18n lib. You just need to set the locale in the request headers.

Docker

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

Winston (log manager)

For manager logs we use winston. Is possible integration with LogStash and others applications.

Eslint and Prettier

Improve code quality and patterns using eslint and prettier.

Jest (unit and integration test)

Improve unit and integration tests using jest pre setup and structure with supertest.

Postman (automate test)

For automate testing, we use Postman to make requests to our APIs.

JWT Auth

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.

ACL - Access Control List

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.

Profile ACL format sample and ACL permission description:

"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)
  } 
}

About

Initial setup for creating a NodeJs application with Rest API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors