0.1.3 • Published 2 years ago

create-benm-app v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

BENM | Boilerplate for REST API

npm.io Codacy Badge Codacy Badge Travis (.com)

A ready-to-use boilerplate for REST API Development with Node.js, Express, and MongoDB

Getting started

This is a basic API skeleton written in JavaScript ES2015. Very useful for building RESTful web APIs for your front-end platforms like Android, iOS or JavaScript frameworks (Angular, ReactJS, etc.).

This project will run on NodeJs using MongoDB as database. I had tried to maintain the code structure easy as any beginner can also adopt the flow and start building an API. Project is open for suggestions, Bug reports and pull requests.

Features

  • Basic Authentication (Register/Login with hashed password)
  • Account confirmation with 4 (Changeable) digit OTP.
  • Email helper ready just import and use.
  • JWT Tokens, make requests with a token after login with Authorization header with value Bearer yourToken where yourToken will be returned in Login response.
  • Pre-defined response structures with proper status codes.
  • Included CORS.
  • Product && User example with CRUD operations.
  • Validations added.
  • Included API collection for Postman.
  • Light-weight project.
  • Postman Collection Link
  • Test cases with Mocha and Chai.
  • Code coverage with Istanbuljs (nyc).
  • Included CI (Continuous Integration) with Travis CI.
  • Linting with Eslint.

Software Requirements

  • Node.js 12+ (Recommended 16+)
  • npm 6+ (Recommended 8+)
  • MongoDB 3.6+ (Recommended 4+)

How to install

Using npm or npx (recommended)

  1. create a BENM app with the npm command. Change "myproject" to your project name.
 npx  create-benm-app myproject

or

 npm init benm-app myproject

Using Git

  1. Clone the project from github. Change "myproject" to your project name.
git clone https://github.com/umagol/NodeJs-ExpressJS-MongoDB-Boilerplate.git ./myproject

Using manual download ZIP

  1. Download the repository
  2. Uncompress to your desired directory

Note: Remove all unwanted files and folders.

Install npm dependencies after installing (Git or manual download)

cd myproject
npm install

Setting up environments (Git or manual download)

  1. You will find a file named .env.example on the project's root directory.
  2. Create a new file by copying and pasting the file and then renaming it to just .env
    cp .env.example .env
  3. The file .env is already ignored, so you never commit your credentials.
  4. Change the values of the file to your environment. Helpful comments were added to the .env.example file to understand the constants.

Project structure

.
├── src
│    ├── bin
│    │   └── www
│    ├── controllers
│    │   ├── AuthController.js
│    │   └── ProductController.js
│    ├── constants
│    │   ├── mailTemplate.js
│    ├── models
│    │   ├── ProductModel.js
│    │   └── UserModel.js
│    ├── routes
│    │   ├── api.js
│    │   ├── auth.js
│    │   ├── privateRouter.js
│    │   ├── publicRouter.js
│    │   └── index.js
│    ├── middlewares
│    │   ├── jwt.js
│    ├── helpers
│    │   ├── apiResponse.js
│    │   └── mailer.js
│    ├── service
│    │   ├── auth.service.js
│    └── utility
│    │   └── utility.js
│    └── app.js
│── public
│    └── index.html
│── test
│    ├── auth.js
│    └── testConfig.js
└── package.json

How to run 🤔

Running API server locally

npm run dev

You will know the server is running by checking the output of the command npm run dev

DataBase connected successfully...
🚀App is running ... 

Press CTRL + C to stop the process.

Note: Update your MongoDB connection string in .env file.

Run application using docker

Run docker using compose file (recommended)

npm run start:docker

Build docker image

npm run build:docker

Run docker image container

npm run start:docker-image

Note: into the docker-compose.yml file update your local configuration. and install docker in your system.

Creating new controllers

If you need to add more controllers to the project just create a new file in /src/controllers/ and use them in the routes.

Creating new models

If you need to add more models to the project just create a new file in /src/models/ and use them in the controllers.

Creating new routes

If you need to add more routes to the project there are three types of routes: 1. private route - The route that requires authentication

**Note:** we have created auth middleware for checking the user token `Bearer token` require.
  1. public route
    • the route that accesses publicly
  1. auth route
    • route related to authentication ( for example login, register, forgot password, etc )

Notes: If you need to add more routes to the project just create a new file in src/routes/ and add it in /routes/api.js it will be loaded dynamically.

Tests

Running Test Cases

npm test

You can set a custom command for a test at the package.json file inside the scripts property. You can also change timeout for each assertion with --timeout parameter of mocha command.

Creating new tests

If you need to add more test cases to the project just create a new file in /test/ and run the command.

ESLint

Running Eslint

npm run lint

You can set custom rules for eslint in the .eslintrc.json file, Added at the project root.

Bugs or improvements

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

License

This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.