Skip to content

A boilerplate RESTful API server using Node.js, Express.js, and MongoDB.

License

Notifications You must be signed in to change notification settings

lquyet/rest-api

Repository files navigation

Node.JS REST API Starter Kit

Author GitHub license GitHub repo size Codacy Badge Github Action

A feature-rich boilerplate for NodeJS RESTful API Development

Getting started

This project provides a skeleton for any further RESTful API development. It implements the 3 Layer Architecture, which are Controller, Service Layer and Data Access Layer.

Controller will handle all stuffs related to requests and responses that are routed by Router. It will then call Service Layer methods which storing all business logic. The Data Access Layer will perform some operations on the database.

Advertise for Work

I am open for a good job or work contract. Contact me with my email quyetlm.work@gmail.com or LinkedIn Minh-Quyet Le.

Features

  • Authentication and Authorization: Bearer JWT Token
  • No-SQL Database: MongoDB (can be installed locally or using a cloud version of MongoDB Atlas)
  • Validation: Ajv JSON schema validator
  • Loging: Morgan
  • Testing: Unit Testing with Mocha and Chai
  • CORS configuration.
  • Light-weight
  • CI/CD: Github Action
  • Linting: Eslint.
  • Error Handling: centralized error handling mechanism
  • Code Quality: Codacy

Environment Requirements

  • Node.js 8+
  • MongoDB 5.0

How to install

Using Git (recommended)

  1. Clone the project
git clone https://github.com/lquyet/rest-api.git ./myproject

Using manual download ZIP

  1. Download repository
  2. Uncompress to your desired directory

Install npm dependencies after installing (Git or manual download)

cd myproject
npm install

Setting up environments

  1. You will find a file named .env.example on root directory of project.

  2. Create a new file by copying and pasting the file and then renaming it to just .env

    cp .env.example .env
  3. Set the appropriate value for your environment variables.

Project structure

rest-api
├─ .eslintrc.json
├─ .github
│  └─ workflows
│     ├─ codeql.yml
│     └─ node.js.yml
├─ .gitignore
├─ app.js
├─ LICENSE
├─ package-lock.json
├─ package.json
├─ README.md
└─ src
   └─ v1
      ├─ authentication
      │  ├─ checkAuth.js
      │  └─ jwt.js
      ├─ controllers
      │  ├─ productController.js
      │  ├─ userController.js
      │  └─ validators
      │     ├─ productValidator.js
      │     └─ userValidator.js
      ├─ database
      │  ├─ models
      │  │  ├─ productModel.js
      │  │  └─ userModel.js
      │  ├─ product.js
      │  └─ user.js
      ├─ routes
      │  ├─ productRoutes.js
      │  └─ userRoutes.js
      ├─ services
      │  ├─ productService.js
      │  └─ userService.js
      └─ test
         ├─ authTest.js
         ├─ config.js
         ├─ productTest.js
         └─ utilityTest.js

How to run

Running API server locally

npm start

Tests

Running Test Cases

npm test

ESLint

Running Eslint

npm run lint

Automatically Fix Eslint error(s)

npm run fix-lint

Custom rules for eslint can be found at .eslintrc.json.

Bugs or improvements

Feel free to report any bugs or improvements by setting a new Issue or Pull request.

License

This project is open-sourced software licensed under the MIT License.