https://github.com/montasim/node-express-boilerplate
A boilerplate/starter project for quickly building RESTful APIs using Node.js, Express, and Mongoose.
https://github.com/montasim/node-express-boilerplate
api-boilerplate api-starter node-backend node-express-boilerplate node-express-mongodb-boilerplate rest-api rest-api-boilerplate rest-api-starter secure-api swagger-api
Last synced: 29 days ago
JSON representation
A boilerplate/starter project for quickly building RESTful APIs using Node.js, Express, and Mongoose.
- Host: GitHub
- URL: https://github.com/montasim/node-express-boilerplate
- Owner: montasim
- License: mit
- Created: 2024-03-15T14:45:15.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-08T05:10:39.000Z (8 months ago)
- Last Synced: 2025-02-09T20:36:44.717Z (3 months ago)
- Topics: api-boilerplate, api-starter, node-backend, node-express-boilerplate, node-express-mongodb-boilerplate, rest-api, rest-api-boilerplate, rest-api-starter, secure-api, swagger-api
- Language: JavaScript
- Homepage: https://node-express-boilerplate-eta.vercel.app
- Size: 2.42 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
[//]: # 'NODE EXPRESS BOILERPLATE'
#
[![]()]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
A boilerplate/starter project for quickly building RESTful APIs using Node.js, Express, and Mongoose.
[//]: # 'CONTENTS'
##
1. [FEATURES](#1-features)
2. [PREREQUISITES](#2-prerequisites)
3. [SETUP](#3-setup)
4. [RUNNING THE SCRIPT](#4-running-the-script)
5. [ERROR HANDLING](#5-error-handling)
6. [HOSTING](#6-hosting)
7. [USED PACKAGES](#7-used-packages)
8. [TOOLS](#8-tools)
9. [ARTICLES](#9-articles)
10. [DO NOT FORGET TO DO](#10-do-not-forget-to-do)
11. [TUTORIALS](#11-tutorials)
12. [INSPIRATIONS](#12-inspirations)
13. [CONTRIBUTE](#13-contribute)
14. [CONTRIBUTORS](#14-contributors)
15. [SPECIAL THANKS](#15-special-thanks)
16. [LICENSE](#16-license)
17. [CONTACT](#17-contact)
[//]: # '1. FEATURES'
##
1. Authentication:
- Handles user login, logout, password resets, and email verification processes.
- Ensures secure access through bearer token authentication.2. User Profile Management:
- Facilitates fetching and updating user profile information based on username. Respects privacy settings and only returns allowed information.
3. Permission and Role Management:
- Manages permissions and roles within the system, allowing for the creation of new roles, modification of existing ones, and deletion of roles based on IDs.
4. Error and Request Handling:
- Includes comprehensive handling of unsupported methods and detailed error responses to ensure robustness and reliability of the API.
### APIs
1. User
- Auth
- Signup: `POST /api/{{VERSION}}/auth/signup`
- Verify signup using email: `GET /api/{{VERSION}}/auth/verify/verification-token`
- Login: `POST /api/{{VERSION}}/auth/login`
- Request password reset email: `PUT /api/{{VERSION}}/auth/request-new-password`
- Resend password reset email: `PUT /api/{{VERSION}}/auth/resend-verification/verification-token`
- Reset password: `PUT /api/{{VERSION}}/auth/reset-password/verification-token`
- Logout: `GET /api/{{VERSION}}/auth/logout`
- Profile
- Update personal information:
- Reset password:
- Delete account:
2. Admin
- Admin
- Create admin: `POST /api/{{VERSION}}/admin`
- Verify admin request using email: `GET /api/{{VERSION}}/admin/verify/verification-token`
- Admin Login: `/api/{{VERSION}}/admin/login`
- Request password reset email: `PUT /api/{{VERSION}}/admin/request-new-password`
- Resend password reset email: `GET /api/{{VERSION}}/admin/resend-verification/token-id`
- Reset password: `PUT /api/{{VERSION}}/admin/reset-password/verification-token`
- Logout: `GET /api/{{VERSION}}/admin/logout`
- Permission
- Create permission: `POST /api/{{VERSION}}/permissions`
- Create default permission: `POST /api/{{VERSION}}/permissions/default`
- Get all permissions: `GET /api/{{VERSION}}/permissions`
- Get permission by ID: `GET /api/{{VERSION}}/permissions/permission-id`
- Update permission by ID: `PUT /api/{{VERSION}}/permissions/permission-id`
- Delete permission by ID: `DELETE /api/{{VERSION}}/permissions/permission-id`
- Delete permission by list: `DELETE /api/{{VERSION}}/permissions?ids=permission-id1,permission-id2`
- Role
- Create role: `POST /api/{{VERSION}}/roles`
- Create default role: `POST /api/{{VERSION}}/roles/default`
- Get all roles: `GET /api/{{VERSION}}/roles`
- Get role by ID: `GET /api/{{VERSION}}/roles/role-id`
- Update role by ID: `PUT /api/{{VERSION}}/roles/role-id`
- Delete role by ID: `DELETE /api/{{VERSION}}/roles/role-id`
- Delete role by list: `DELETE /api/{{VERSION}}/roles?ids=role-id1,role-id2`
3. Public
- Detect
- User device detection
[//]: # 'PREREQUISITES'
[//]: # '2. PREREQUISITES'##
1. Node.js (v20.x or higher)
2. Yarn (v1.22.x or higher)Ensure you have `Node.js` and `Yarn` installed by running `node -v` and `yarn -v` in your terminal. These commands will display the current version of each installed on your system. If these are not installed, follow the installation instructions on the [Node.js website](https://nodejs.org/) and the [Yarn website](https://classic.yarnpkg.com/en/).
[//]: # '3. SETUP'
##
1. **Clone the repository and navigate to the directory:**
```bash
git clone https://github.com/montasim/node-express-boilerplate.git
cd node-express-boilerplate
```2. **Install the dependencies:**
```bash
yarn install
```3. **Configuring the Environment:**
Create a `.env.development` or `.env.staging` or `.env.production` file in the root directory of the project and populate it with the necessary environment variables. See the [.env.example](.env.example) file for an example.
[//]: # '4. RUNNING THE SCRIPT'
##
1. **Running the Application:**
To start the application in development mode, use:
```bash
yarn dev
```This will run the server with nodemon, automatically restarting when any changes are made.
2. **To build and run the application in production mode, use:**
```bash
yarn start
```This will build the application and start the server using the built files.
This script first builds the project by linting the code, fixing lint issues, running prettier, and then starts the application with pm2.
3. **Testing:**
To run the tests configured with Jest, use:
```bash
yarn test
```This will build the project and then run all the Jest tests.
4. **Linting and Code Formatting:**
- To check for linting errors:
```bash
yarn lint:check
```- To fix linting errors:
```bash
yarn lint:fix
```- To check if files are formatted correctly:
```bash
yarn prettier:check
```- To format files:
```bash
yarn prettier:fix
```5. **Generating Documentation:**
To generate code documentation with JSDoc, run:
```bash
yarn generate-docs
```This will create documentation based on your JSDoc comments.
6. **Release Management:**
To create a new release, you can use:
```bash
yarn release
```This will automatically bump the version, update the CHANGELOG, and create a commit and a tag.
For minor or major releases:
```bash
yarn release:minor
yarn release:major
```7. **Cleanup:**
To clean up dependencies and rebuild the project:
```bash
yarn clean
```This command initializes the auto cleanup process and then forces a rebuild.
### Docker
1. **Build the Docker Compose Services:**
```bash
yarn docker:build-dev
```2. **Run the Docker Compose Services:**
```bash
yarn docker:run-dev
```3. **Stop the Containers:**
```bash
yarn docker:stop-dev
```4. **Rebuild the Containers:**
```bash
yarn docker:rebuild-dev
```[//]: # '5. ERROR HANDLING'
##
[//]: # '6. HOSTING'
##
[](https://vercel.com/import/project?template=https://github.com/montasim/node-express-boilerplate)
Step-by-step guide on setting up your own Vercel instance:
Vercel is the recommended option for hosting the files since it is free and easy to set up.
1. Go to [vercel.com](https://vercel.com/).
2. Click on `Log in`.

3. Sign in with GitHub by pressing `Continue with GitHub`.

4. Sign in to GitHub and allow access to all repositories if prompted.
5. [Fork this repo.](https://github.com/montasim/node-express-boilerplate/fork)
6. Go back to your [Vercel dashboard](https://vercel.com/dashboard).
7. To import a project, click the `Add New...` button and select the `Project` option.

8. Click the `Continue with GitHub` button, search for the required Git Repository and import it by clicking the `Import` button. Alternatively, you can import a Third-Party Git Repository using the `Import Third-Party Git Repository ->` link at the bottom of the page.

9. Create a personal access token (PAT) [here](https://github.com/settings/tokens/new) and enable the `repo` and `user` permissions (this allows access to see private repo and user stats).
10. Copy all the .env.development file as environment variables in the Vercel dashboard.
11. Click deploy, and you're good to go. See your domains to use the API!
[//]: # '7. USED PACKAGES'
##
List of used packages:
1. `Node.js (v20.x or higher)`: The runtime environment for executing JavaScript on the server side.
2. `Express.js (v4.19.x or higher)`: The web application framework for creating server-side logic.
3. `MongoDB (v6.8.x or higher)`: The NoSQL database for storing and retrieving data.
4. `@googleapis/drive (v8.11.0)`: A library for interacting with Google Drive API.
5. `bcrypt (v5.1.1)`: A library to help you hash passwords.
6. `compression (v1.7.4)`: Middleware to compress response bodies for all requests.
7. `cors (v2.8.5)`: Middleware to enable Cross-Origin Resource Sharing.
8. `dompurify (v3.1.5)`: A library to sanitize HTML to prevent XSS attacks.
9. `dotenv (v16.4.5)`: A module to load environment variables from a .env file.
10. `express-useragent (v1.0.15)`: A user agent middleware for Express.
11. `glob (v10.4.2)`: A library to match files using the patterns the shell uses.
12. `helmet (v7.1.0)`: Middleware to secure Express apps by setting various HTTP headers.
13. `hpp (v0.2.3)`: Middleware to protect against HTTP Parameter Pollution attacks.
14. `jest (v29.7.0)`: A testing framework for JavaScript.
15. `joi (v17.13.3)`: A library for data validation.
16. `jsdom (v24.1.0)`: A library to simulate a web browser environment in Node.js.
17. `jsonwebtoken (v9.0.2)`: A library to sign, verify, and decode JSON Web Tokens.
18. `moment (v2.30.1)`: A library for parsing, validating, manipulating, and formatting dates.
19. `mongodb (v6.8.0)`: MongoDB driver for Node.js.
20. `mongoose (v8.4.4)`: An ODM (Object Data Modeling) library for MongoDB and Node.js.
21. `morgan (v1.10.0)`: HTTP request logger middleware for Node.js.
22. `multer (v1.4.5-lts.1)`: Middleware for handling multipart/form-data, used for file uploads.
23. `node-cache (v5.1.2)`: A caching library for Node.js.
24. `nodemailer (v6.9.14)`: A module for sending emails.
25. `pm2 (v5.4.1)`: A production process manager for Node.js applications.
26. `request-ip (v3.3.0)`: A library to retrieve a request's IP address.
27. `supertest (v7.0.0)`: A library for testing HTTP assertions.
28. `terser (v5.31.1)`: A JavaScript parser, mangler, and compressor toolkit for ES6+.
29. `uuid (v10.0.0)`: A library to generate RFC4122 UUIDs.
30. `winston (v3.13.0)`: A logging library for Node.js.
31. `winston-daily-rotate-file (v5.0.0)`: Transport for winston to log to daily rotated files.
32. `winston-mongodb (v5.1.1)`: Transport for winston to log to a MongoDB collection.### Development Dependencies
1. `@babel/core (v7.24.7)`: The core of Babel, a JavaScript compiler.
2. `@babel/preset-env (v7.24.7)`: A Babel preset to compile ES6+ down to ES5.
3. `eslint (v9.5.0)`: A tool for identifying and reporting on patterns in JavaScript.
4. `jsdoc (v4.0.3)`: A tool for generating documentation from JavaScript source code.
5. `nodemon (v3.1.4)`: A utility that will monitor for any changes in your source and automatically restart your server.
6. `prettier (v3.3.2)`: An opinionated code formatter.
7. `standard-version (v9.5.0)`: A utility for versioning and changelog management.
8.
[//]: # '8. TOOLS'
##
1. [WebStorm](https://www.jetbrains.com/webstorm/)
2. [Postman](https://www.postman.com/)
3. [Swagify.io](https://swagify.io/convert/)
[//]: # '9. ARTICLES'
##
1. [StackOverflow answer on file upload to Google Drive](https://stackoverflow.com/questions/65181932/how-i-can-upload-file-to-google-drive-with-google-drive-api).
[//]: # '10. DO NOT FORGET TO DO'
##
[//]: # '11. TUTORIALS'
##
1. [Google Drive file upload tutorial](https://www.youtube.com/watch?v=bkaQTLCBBeo&t=600s).
[//]: # '12. INSPIRATIONS'
##
[//]: # '13. CONTRIBUTE'
##
Contributions are always welcome!
Please read the [contribution guidelines](CONTRIBUTION.md) and [contributor license agreement](CLA.md).
[//]: # '14. CONTRIBUTORS'
##
[//]: # '15. SPECIAL THANKS'
##
[//]: # '16. LICENSE'
##
[](./LICENSE)
[//]: # '17. CONTACT'
##