A robust Node.js backend built with Express.js, TypeScript, and MongoDB.
- 🔐 Authentication & Authorization: JWT-based authentication with role-based access control
- 🛡️ Security: Helmet, CORS, rate limiting, XSS protection, and input sanitization
- 📝 Validation: Request validation and sanitization
- 🚨 Error Handling: Comprehensive error handling with custom error classes
- 🗄️ Database: MongoDB with Mongoose ODM
- 🔄 TypeScript: Full TypeScript support with strict type checking
- 📊 Logging: Request logging and error tracking
- 🏗️ Architecture: Clean architecture with separation of concerns
src/
├── config/ # Configuration files
│ ├── db.ts # Database connection
│ └── envConfig.ts # Environment variables
├── controllers/ # Route controllers
│ └── authController.ts
├── middleware/ # Custom middleware
│ ├── auth.ts # Authentication middleware
│ ├── errorHandler.ts # Error handling
│ └── validation.ts # Request validation
├── models/ # Database models
│ └── User.ts
├── routes/ # API routes
│ ├── auth.ts # Authentication routes
│ └── index.ts # Main routes
├── services/ # Business logic
└── server.ts # Main server file
- Node.js (v16 or higher)
- MongoDB (local or cloud instance)
- npm or yarn
- Clone the repository:
git clone <repository-url>
cd backend
- Install dependencies:
npm install
- Create environment file:
cp env.example .env
- Configure environment variables in
.env
:
NODE_ENV=development
PORT=3000
MONGO_URI=mongodb://localhost:27017/your_database_name
JWT_SECRET=your_super_secret_jwt_key_here_make_it_long_and_random
JWT_EXPIRES_IN=7d
CORS_ORIGIN=http://localhost:3000
- Build the project:
npm run build
- Start the development server:
npm run dev
- POST
/api/auth/register
- Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"role": "user"
}
- POST
/api/auth/login
- Body:
{
"email": "john@example.com",
"password": "password123"
}
- GET
/api/auth/me
- Headers:
Authorization: Bearer <token>
- POST
/api/auth/logout
- Headers:
Authorization: Bearer <token>
- GET
/api/health
- Helmet: Security headers
- CORS: Cross-origin resource sharing
- Rate Limiting: 100 requests per 10 minutes per IP
- XSS Protection: Prevents cross-site scripting attacks
- Input Sanitization: MongoDB query injection protection
- HTTP Parameter Pollution: Prevents parameter pollution attacks
The API uses a centralized error handling system with custom error classes:
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
- 500: Internal Server Error
npm run dev
: Start development server with hot reloadnpm run build
: Build the projectnpm start
: Start production server
Variable | Description | Default |
---|---|---|
NODE_ENV |
Environment mode | development |
PORT |
Server port | 3000 |
MONGO_URI |
MongoDB connection string | Required |
JWT_SECRET |
JWT signing secret | Required |
JWT_EXPIRES_IN |
JWT expiration time | 7d |
CORS_ORIGIN |
CORS allowed origin | http://localhost:3000 |
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
ISC