Building and Sharing a Robust Node.js MongoDB Boilerplate for Modern Web Applications

Saniaalikhan
3 min readAug 21, 2024

--

Introduction

As a developer, starting a new project often involves setting up the foundational structure of your application. This initial setup can be time-consuming, especially when working with Node.js and MongoDB, where configuring the environment, connecting to the database, setting up routes, and ensuring code quality are essential but repetitive tasks. To streamline this process, I’ve developed a comprehensive Node.js MongoDB boilerplate that integrates modern practices and tools, allowing you to focus more on writing business logic and less on setup.

In this article, I’ll walk you through the features of this boilerplate, how it’s structured, and how you can use it to kickstart your next project.

Why I Built This Boilerplate

Starting with a clean slate is often ideal, but repetitive tasks such as setting up environment variables, configuring MongoDB, creating initial API routes, and setting up a linting/formatting pipeline can be tedious. I built this boilerplate to:

  1. Save Time: Eliminate the need to manually configure these essential components every time I start a new project.
  2. Ensure Consistency: Maintain a consistent structure across projects, which is particularly useful when working in teams.
  3. Promote Best Practices: Integrate best practices in Node.js development, including the use of ES modules, environment variables, and Swagger documentation.

Key Features

This boilerplate is packed with features that are essential for modern web application development:

  • Express.js: A fast, unopinionated web framework for Node.js that provides robust routing and middleware solutions.
  • MongoDB with Mongoose: Simplifies data modeling, schema validation, and database interaction with MongoDB.
  • ES Modules: Utilizing modern JavaScript ES6+ syntax for a more modular and cleaner codebase.
  • Environment Variables: Managed through .env files, making configuration flexible and secure.
  • Swagger UI Integration: Automatically generate API documentation that’s interactive and easy to use.
  • Nodemon for Development: Automatically restart your server on code changes, ensuring a smooth development experience.
  • Prettier & ESLint: Maintain code quality and consistency using Airbnb’s style guide integrated with ESLint and Prettier.
  • AWS S3 Integration: Seamlessly handle file uploads to AWS S3, making the boilerplate suitable for applications requiring media storage.

Folder Structure

The boilerplate is organized into a clean and intuitive folder structure:

.
├── config
│ ├── config.js # Environment variable configuration
│ └── swaggerConfig.js # Swagger configuration
├── connection
│ └── connect.js # MongoDB connection setup
├── controller # Controllers for handling business logic
├── Models # Mongoose models
├── Routes # API routes
├── .env # Environment variables (gitignored)
├── .env-sample # Sample environment file
├── .eslintrc.json # ESLint configuration
├── .gitignore # Files and directories to ignore in Git
├── .prettierrc # Prettier configuration
├── api.http # HTTP requests for testing
├── app.js # Main application entry point
├── index.js # Index file to start the server
├── package.json # Project dependencies and scripts
├── swagger.yml # Swagger documentation in YAML
└── vercel.json # Vercel deployment configuration

Getting Started

  1. Clone the Repository: Start by cloning the repository to your local machine:

git clone https://github.com/your-username/node-mongo-boilerplate.git
cd node-mongo-boilerplate

Install Dependencies: After cloning, install the necessary dependencies:

npm install

Set Up Environment Variables: Create a .env file by copying the provided .env-sample and update it with your specific configuration:

DB_URI=mongodb://localhost:27017/your-db-name
PORT=5000
JWT_SECRET=your_jwt_secret
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_BUCKET_NAME=your_s3_bucket_name

Run the Application: Use the following command to start the server:

npm start

Conclusion

This Node.js MongoDB boilerplate is designed to streamline the setup process for new projects, ensuring that you can focus on writing business logic rather than boilerplate code. By integrating essential features like environment configuration, database connection, and API documentation, this boilerplate provides a solid foundation for your next web application.

Feel free to fork the repository, make it your own, and start building amazing applications with it. Contributions and feedback are welcome!

--

--

Saniaalikhan
Saniaalikhan

Written by Saniaalikhan

Tech Entrepreneur and Web Engineer driving innovation and business growth through technology. Follow: LinkedIn https://www.linkedin.com/in/sania-khan-242677119/

No responses yet