Skip to content

This is a production-ready skeleton for building RESTful APIs in Node.JS using Express and MongoDB.

License

Notifications You must be signed in to change notification settings

dejosli/NodeJS-RESTful-API-Boilerplate

Repository files navigation

NodeJS RESTful API Boilerplate

This is a production-ready skeleton for building RESTful APIs in Node.JS using Express and MongoDB. The skeleton has many built-in features, such as authentication using JWT, request validation, unit and integration tests, continuous integration, API documentation, etc. For more details, please check the features list below.

Features

Requirements

Quick Start

Clone the repo and make it yours

git clone https://github.com/dejosli/NodeJS-RESTful-API-Boilerplate.git
cd NodeJS-RESTful-API-Boilerplate
rm -rf .git

Install Yarn

npm install -g yarn # if yarn is not installed

Install dependencies

yarn install
or
yarn install --frozen-lockfile # recommended

Set environment variables

cp .env.example .env

# open .env and modify the environment variables (if needed)

Folder Structure

src
│   index.js
│
├───api
│   ├───controllers
│   │   └───tests
│   ├───docs
│   ├───lib
│   │   └───tests
│   ├───middleware
│   ├───models
│   │   └───plugins
│   ├───routes
│   │   └───v1
│   ├───services
│   │   └───tests
│   ├───utils
│   │   └───tests
│   ├───validators
│   │   └───tests
│   └───workers
├───config
├───core
└───tests

How to Run

Running Locally

yarn dev

Running in Production

yarn start

Response Format

Success Response

{
  "success": true,
  "code": 200,
  "message": "Success Response Message",
  "data": {}
}

Error Response

{
  "success": false,
  "code": 400,
  "message": "Error Response Message",
  "errors": [
    {
      "field": "phoneNumber",
      "message": "Phone number already exists for another user."
    }
  ]
}

Commands

Linting

Linting is done using ESLint and Prettier.

# lint code with ESLint
yarn lint

# try to fix ESLint errors
yarn lint:fix

# style code with Prettier
yarn prettier

# try to fix Prettier errors
yarn prettier:fix

In this project, ESLint is configured to follow the Airbnb JavaScript style guide with some modifications. It also extends eslint-config-prettier and eslint-plugin-security to turn off all rules that are unnecessary or might conflict with Prettier.

To modify the ESLint configuration, update the .eslintrc.json file. To modify the Prettier configuration, update the .prettierrc.json file.

To prevent a certain file or directory from being linted, add it to .eslintignore and .prettierignore.

To maintain a consistent coding style across different IDEs, the project contains .editorconfig

Testing

# run all tests
yarn test

# run all tests in watch mode
yarn test:watch

# run test coverage
yarn test:coverage

# run test coverage with coveralls
yarn test:coveralls

Conventional Commits

# add all changes to staged
git add .

# run for conventional commits message
yarn commit

Husky Install

yarn prepare

List of things that need to be done

  • Test the utilities
  • Test the libraries
  • Test the validators
  • Test the middleware
  • Test the services
  • Test the controllers
  • Test the API's endpoints

Inspirations

Bugs or improvements

Every project needs improvements, Feel free to report any bugs or improvements. Pull requests are always welcome.

License

Open-sourced software licensed under the MIT License

About

This is a production-ready skeleton for building RESTful APIs in Node.JS using Express and MongoDB.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published