A boilerplate project for creating backend applications using TypeScript, Express, MongoDB, and Node.js. This template is designed to help you quickly set up new projects with a clean and scalable structure.
- TypeScript for static type checking and cleaner code.
- Express for fast, unopinionated web framework for Node.js.
- MongoDB for database storage.
- Mongoose for MongoDB object modeling.
- dotenv for environment variable management.
- CORS enabled.
- Structured with models, controllers, and routes for easy scalability.
src/
├── controllers/ # Controllers for handling business logic
├── models/ # Mongoose models for MongoDB collections
├── routes/ # Express routes
├── config/ # Configuration files (if needed)
├── app.ts # Main Express app configuration
└── server.ts # Server start and MongoDB connection logic
- Node.js (>= 18.x)
- MongoDB
-
Clone the repository:
git clone https://github.com/your-username/ts-node-mongo-base.git cd ts-node-mongo-base
-
Install dependencies using pnpm:
pnpm install
-
Set up environment variables. Create a
.env
file in the root directory and add the following:PORT=5000 MONGO_URI=mongodb://localhost:27017/mydatabase
-
Run the development server:
pnpm run dev
-
To build the project for production:
pnpm run build pnpm start
Method | Endpoint | Description |
---|---|---|
GET | /api/users |
Get all users |
POST | /api/users |
Create a new user |
{
"name": "John Doe",
"email": "john@example.com"
}
pnpm run dev
- Runs the app in development mode withnodemon
andts-node
.pnpm run build
- Builds the TypeScript code into JavaScript.pnpm start
- Starts the production server.
You can integrate ESLint and Prettier to maintain clean code, but it's optional.
This template includes a unified response structure and global error handling for a consistent API.
export const sendResponse = (
res: Response,
success: boolean,
message: string = "Success",
data: any = null,
statusCode: number = 200
) => {
return res.status(statusCode).json({
success,
message,
data,
});
};