A boilerplate for building scalable backend applications using Node.js, Express, Mongoose, and TypeScript with built-in tools for better code quality and development workflow.
- TypeScript: Strongly typed JavaScript for improved developer productivity and maintainability.
- Mongoose: Object Data Modeling (ODM) library for MongoDB, simplifying database operations.
- ESLint: Static code analysis to maintain a clean and consistent codebase.
- Prettier: Code formatter to enforce a uniform code style.
- Husky: Pre-commit hooks to ensure code quality checks before committing.
- Global Error Handler: Centralized error handling for better maintainability and debugging.
- Environment Configuration: Manage environment-specific variables securely.
Ensure you have the following installed:
- Clone this repository:
git clone https://github.com/sajidul-dev/express-typescript-mongoose-starter.git
- Navigate to the project directory:
cd express-typescript-mongoose-starter
- Install dependencies:
npm install #or yarn install
-
Copy
.env.example
to.env
and configure the environment variables:cp .env.example .env
-
Start the development server:
npm run dev #or yarn dev
-
Build and run for production:
npm run build npm start #or yarn build yarn start
Scripts | Description |
---|---|
npm run dev or yarn dev |
Run the app in development mode. |
npm run build or yarn build |
Build the app for production. |
npm start or yarn start |
Run the built app in production mode. |
npm run lint:check or yarn lint:check |
Check code for linting issues. |
npm run lint:fix or yarn lint:fix |
Fix linting issues automatically. |
npm run prettier:check or yarn prettier:check |
Format code using Prettier. |
- Enforces code quality and consistency.
- Run
npm run lint:check
to check for issues.
- Automatically formats code to maintain uniform style.
- Run
npm run prettier:check
to format the entire codebase.
- Pre-commit hooks to prevent bad code from being committed.
- Includes linting and formatting checks before each commit.
- Centralized middleware for handling errors across the application.
- Ensures consistent error responses and detailed debugging information in development.
- TypeScript-first schema validation.
The project is organized into the following structure:
src/
|
├── app/
│ ├── middleware/ # Express middleware
│ │ ├── globalErrorHandler.ts # Error handler for entire application
│ │ └── validateRequest.ts #
│ ├── modules/ # Feature modules
│ │ └── user/
├── config/ # Application configuration
│ │ └── index.ts # (e.g., database, environment)
├── errors/
│ │ ├── ApiError.ts # Common error response
│ │ └── handleZodError.ts # Common zod error handler
├── interfaces/
│ │ ├── common.ts # All common interfaces
│ │ └── error.ts # Common error interfaces
├── shared/
│ │ ├── catchAsync.ts # Handler for async function
│ │ └── sendResponse.ts # Common response
├── app.ts # Express app setup
├── app.ts # Express app setup
│── server.ts # Server entry point
├── .env
├── .env.example
├── package.json
└── tsconfig.json