create-benm-app
v0.1.3
Published
Boilerplate for REST API with Node.js, ExpressJS, and MongoDB
Downloads
4
Maintainers
Readme
BENM | Boilerplate for REST API
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 valueBearer yourToken
whereyourToken
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)
- 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
- 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
- Download the repository
- 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)
- You will find a file named
.env.example
on the project's root directory. - Create a new file by copying and pasting the file and then renaming it to just
.env
cp .env.example .env
- The file
.env
is already ignored, so you never commit your credentials. - 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:
private route
- The route that requires authentication
Note: we have created auth middleware for checking the user token
Bearer token
require.public route
- the route that accesses publicly
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.