A boilerplate project for building backend applications using Node.js, TypeScript, and MongoDB. This repository provides a well-structured foundation with commonly used features and configurations.
- TypeScript for type-safe development.
- Express.js for building RESTful APIs.
- MongoDB integration with the official MongoDB driver.
- Modular folder structure for scalability.
- Environment-based configurations using
.env. - Seeder support for initializing data.
- Predefined scripts for development, building, and seeding.
project-root/
|-- dist/ # Compiled JavaScript files
|-- src/ # Main source files
| |-- configs/ # Configuration files
| |-- constants/ # App constants
| |-- controllers/ # Route controllers
| |-- emails/ # Email-related functionality
| |-- middlewares/ # Express middlewares
| |-- models/ # MongoDB models
| |-- routes/ # API routes
| |-- seeder/ # Data seeders
| |-- services/ # Business logic services
| |-- types/ # TypeScript types
| |-- utils/ # Utility functions
| |-- validators/ # Input validation logic
| |-- app.ts # Express app configuration
| |-- server.ts # Starting point of the server
|-- .env.example # Environment variable example
|-- package.json # Project dependencies and scripts
|-- tsconfig.json # TypeScript configuration
express: Fast, unopinionated, minimalist web framework for Node.js.dotenv: Loads environment variables from a.envfile intoprocess.env.mongodb: Official MongoDB driver for Node.js.mongoose: Elegant MongoDB object modeling for Node.js.multer: Middleware for handlingmultipart/form-datafor file uploads.winston: Versatile logging library.
joi: Schema description language and data validator for JavaScript.
typescript: TypeScript compiler.ts-node: TypeScript execution environment for Node.js.nodemon: Monitors changes and restarts the server automatically during development.eslint: Pluggable JavaScript linter.@typescript-eslint/*: Linting for TypeScript files.@types/*: TypeScript definitions for the required packages (e.g.,@types/express,@types/mongodb, etc.).
-
Clone the repository:
git clone https://github.com/itsahmadawais/node-ts-mongo-starter.git cd node-ts-mongo-starter -
Install dependencies:
npm install
-
Configure environment variables:
- Copy the
.env.examplefile and rename it to.env. - Set the required variables:
PORT=3000 MONGODB_URI=mongodb://localhost:27017/your-database-name
- Copy the
-
Run the development server:
npm run dev
The server will start at
http://localhost:<PORT>.
| Command | Description |
|---|---|
npm run build |
Compile TypeScript to JavaScript in /dist |
npm start |
Start the production server |
npm run dev |
Start the development server with nodemon |
npm run seed |
Seed the database with initial data |
Define your routes in src/routes/ and corresponding logic in src/controllers/.
Define MongoDB models in src/models/ using the MongoDB Node.js driver.
Add custom middleware in src/middlewares/.
Use the src/seeder/ directory to create scripts for initializing the database with default data.
Run them using:
npm run seed- Fork the repository.
- Create a new branch.
- Make your changes and commit.
- Push to your branch and create a pull request.
This project is licensed under the MIT License.
Happy coding!