I'm solidifying the core architecture for the boilerplate. Express, TypeScript, and MongoDB are the cornerstones. I'm focusing on clean structure with robust type definitions to streamline development. Considering essential middleware and initial routes.
Outlining the Structure
I'm now detailing the directory structure, creating distinct folders for configurations, controllers, models, routes, middleware, and utilities within the src/ directory. I've also outlined app.ts and server.ts for the main setup and entry point. I've chosen Express.js, MongoDB via Mongoose, and strong typing is planned throughout, supported by hot reloading. Configuration with environment variables and consistent build, linting, and formatting practices are set.
Detailing the Setup
I've outlined the necessary dependencies for both production and development environments, including Express, Mongoose, TypeScript, and related type definitions. I'm configuring the tsconfig.json for strict mode and specifying the output directory. Now, I'm working on the essential environment variables, MongoDB connection, a sample user model, controllers, and routes. I'm setting up the application entry point and defining scripts for development, building, and starting the server.
Clarifying the Dependencies
I'm solidifying the configuration of the development and production dependencies, and have chosen to keep it simple, checking for availability for the environment variables to keep it simple. After some thoughts, I decided to go with a straightforward approach for error handling, with a basic error middleware.
Defining the Structure
I'm now focusing on setting up the database connection using async/await and defining a User model with an interface. I've designed the user controller and routes, and integrated middleware like CORS and express.json within app.ts. Also, I've organized the server setup within server.ts. I'm reviewing the structure against best practices now, ensuring separation of concerns, proper interface definitions for models, and the use of dotenv. I'm also adding security features like helmet and CORS. Finally, I will add instructions for running the boilerplate and describe the functionality of the scripts. I need to remember to instruct people to create the .env file, and I'll use rimraf for clean builds.
Here is a complete, production-ready boilerplate for Express, TypeScript, and MongoDB (Mongoose). It follows standard industry practices such as separation of concerns (MVC pattern), strict typing, and environmental configuration.
1. Project Setup
First, create your folder and initialize the project.
bash
mkdir express-ts-mongo-api
cd express-ts-mongo-api
npm init -y
2. Install Dependencies
Production Dependencies:
bash
npm install express mongoose dotenv cors helmet morgan
import express, { Application, Request, Response } from 'express';
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import userRoutes from './routes/userRoutes';
import errorHandler from './middleware/errorHandler';
const app: Application = express();
// Middleware
app.use(express.json());
app.use(cors());
app.use(helmet());
app.use(morgan('dev'));
// Routes
app.use('/api/users', userRoutes);
// Health Check
app.get('/', (req: Request, res: Response) => {
res.send('API is running...');
});
// Error Handling
app.use(errorHandler);
export default app;
src/server.ts
The entry point for the server.
typescript
import dotenv from 'dotenv';
import connectDB from './config/db';
import app from './app';
// Load env vars
dotenv.config();
// Connect to database
connectDB();
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`);
});
6. Scripts
Update your package.json scripts section to handle builds and development mode.