This is a boilerplate template for a Node.js project with a clean folder structure using Mongoose and MongoDB. The structure is organized to keep the code modular and maintainable.
The project structure is as follows:
project-root
│
├── .env.example
├── package.json
├── tsconfig.json
├── docker-compose.yml
└── src
├── Interfaces
│ └── (Interface files go here)
│
├── Models
│ └── (Database models go here)
│
├── Modules
│ └── User
│ ├── controllers
│ │ ├── index.ts
│ │ └── (Controller files go here)
| |
| ├── middlewares
│ │ ├── index.ts
│ │ └── (Middleware files go here)
│ │
│ ├── repositories
│ │ ├── index.ts
│ │ └── (Repository files go here)
│ │
│ ├── services
│ │ ├── index.ts
│ │ └── (Service files go here)
│ │
│ ├── routes
│ │ ├── index.ts
│ │ └── (Route files go here)
│ │
│ └── index.ts (Exports the user module)
|
├── utils
| └── (Utility functions go here)
|
├── Dockerfile.dev
└── index.ts
Ensure you have the following installed on your local development environment:
- Node.js (v14.x or higher)
- MongoDB (v4.x or higher)
- Docker (if you want to use Docker for development)
-
Clone the repository:
git clone https://github.com/your-username/your-repo.git cd your-repo
-
Install dependencies:
npm install
-
Set up environment variables:
Copy the
.env.example
file to a new file named.env
in the root of your project:cp .env.example .env
Edit the
.env
file to include your MongoDB connection string and other environment-specific variables:MONGODB_URL="YOUR MONGODB URL" NODE_ENV="dev" PORT=10000 SELF_URL="http://localhost:10000/ping"
-
Run MongoDB locally:
If MongoDB is not already running, start it using:
mongod --dbpath /path-to-your-db-data
Or, if you prefer using Docker, you can spin up a MongoDB container using the provided
docker-compose.yml
:docker-compose up -d
-
Start the development server:
npm run dev
This will start the server with nodemon, automatically reloading on changes.
-
Access the application:
Open your browser and navigate to
http://localhost:10000
.
-
src/Interfaces: This folder contains TypeScript interfaces for data models.
-
src/Models: This folder contains Mongoose schemas and models.
-
src/Modules: This folder contains the application's feature modules. Each module (e.g.,
user
) is organized into subfolders forcontrollers
,repositories
,services
, androutes
.-
controllers: Handle incoming HTTP requests and interact with the service layer.
-
repositories: Encapsulate the logic required to access data sources.
-
services: Contain the business logic of the application.
-
routes: Define the API endpoints and link them to controller functions.
-
-
src/utils: Utility functions used across the application.
To customize this boilerplate for your own use:
-
Add new modules:
- Use the
@techymt/create-module
package to generate new modules. - Define your models, controllers, services, and routes in the respective directories.
- Use the
-
Modify the Mongoose models:
- Update the schemas in
src/Models
to match your application's data structure. - Ensure any new models are exported and used within your services.
- Update the schemas in
Contributions are welcome! Please open an issue or submit a pull request for any improvements.
This project is licensed under the MIT License. See the LICENSE file for details.