create-mvc-backend-app
v1.0.6-beta
Published
A complete backend API template following MVC architecture with Node.js, Express.js, MongoDB, and JWT authentication
Maintainers
Readme
Backend MVC Template
A complete backend API template following MVC architecture with Node.js, Express.js, MongoDB, and JWT authentication. Perfect for rapid backend development and learning MVC patterns.
📋 Prerequisites
Before you begin, ensure you have the following installed:
- Node.js (version 14.0.0 or higher) - Download here
- MongoDB - Download here or use MongoDB Atlas (cloud)
- npm (comes with Node.js)
🚀 Quick Start
Method 1: Using NPX (Recommended)
# Create a new backend project with npx (no installation needed!)
npx create-mvc-backend-app my-awesome-backendProject Name Options:
- Enter a name (e.g.,
my-backend): Creates a new directory with that name - Enter
.: Uses the current directory (like Vite)
Alternative Usage:
# Run without project name (will prompt you)
npx create-mvc-backend-app
# Use current directory
npx create-mvc-backend-app .What happens when you run the command:
🚀 MVC Backend App Generator
============================
Project name: my-awesome-backend
📁 Creating project: my-awesome-backend
📂 Created directory: my-awesome-backend
✅ Copied src
✅ Copied server.js
✅ Copied env.example
✅ Copied README.md
✅ Copied LICENSE
✅ Created package.json
🎉 Project created successfully!
Next steps:
1. cd my-awesome-backend
2. npm install
3. cp env.example .env
4. Update .env with your MongoDB URI and JWT secret
5. npm run dev
Happy coding! 🚀Method 2: Clone from GitHub (Without npm)
# Clone the repository
git clone https://github.com/DeveloperChetram/backend-setup.git my-backend-project
# Navigate to the project directory
cd my-backend-project
# Install dependencies
npm install⚙️ Setup & Configuration
1. Environment Variables
# Copy the environment template
cp env.example .env2. Configure Environment Variables
Update your .env file with the following:
# Database Configuration
MONGODB_URI=mongodb://localhost:27017/your-database-name
# For MongoDB Atlas: mongodb+srv://username:[email protected]/database-name
# JWT Secret (generate a strong secret key)
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
# Server Configuration
PORT=5000
NODE_ENV=development
# Frontend URL (for CORS)
FRONTEND_URL=http://localhost:3000
# Cookie Configuration
COOKIE_EXPIRES_IN=5d3. MongoDB Setup
Option A: Local MongoDB
- Install MongoDB locally
- Start MongoDB service
- Use
mongodb://localhost:27017/your-database-namein your.env
Option B: MongoDB Atlas (Cloud)
- Create a free account at MongoDB Atlas
- Create a new cluster
- Get your connection string
- Replace
<password>and<dbname>in the connection string - Use the connection string in your
.env
4. Start the Server
# Development mode (with auto-restart)
npm run dev
# Production mode
npm startThe server will start on http://localhost:5000 (or your specified PORT).
✨ Features
- ✅ User Registration - Complete user signup with validation
- ✅ User Login - Secure authentication with JWT
- ✅ User Logout - Proper session termination
- ✅ JWT Authentication - Secure token-based auth
- ✅ HTTP-only Cookies - XSS protection
- ✅ Password Hashing - bcryptjs security
- ✅ Input Validation - Server-side validation
- ✅ Error Handling - Consistent error responses
- ✅ CORS Configuration - Cross-origin support
- ✅ MongoDB Integration - Mongoose ODM
- ✅ MVC Architecture - Clean code organization
📁 Project Structure
backend-mvc-template/
├── src/
│ ├── controllers/ # Request handlers
│ │ └── auth.controllers.js
│ ├── models/ # Database schemas
│ │ └── user.model.js
│ ├── routes/ # API route definitions
│ │ └── auth.routes.js
│ ├── middlewares/ # Cross-cutting concerns
│ │ └── auth.middleware.js
│ ├── db/ # Database connection
│ │ └── db.js
│ ├── utils/ # Utility functions
│ │ └── cookieOptions.js
│ └── app.js # Express app configuration
├── server.js # Server entry point
├── package.json # Dependencies and scripts
├── env.example # Environment variables template
└── README.md # This fileAPI Endpoints
Authentication
| Method | Endpoint | Description | Body |
|--------|----------|-------------|------|
| POST | /api/auth/register | Register new user | { name, email, password } |
| POST | /api/auth/login | Login user | { email, password } |
| GET | /api/auth/logout | Logout user | - |
Health Check
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | / | Server health check |
| GET | /api/health | Server health check |
API Response Format
Success Response
{
"success": true,
"message": "Operation completed successfully",
"data": {
"user": {
"_id": "user_id",
"name": "User Name",
"email": "[email protected]",
"isActive": true,
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
}
}Error Response
{
"success": false,
"message": "Error description"
}Example Usage
Register User
curl -X POST http://localhost:5000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}'Login User
curl -X POST http://localhost:5000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "password123"
}'Logout User
curl -X GET http://localhost:5000/api/auth/logout \
-H "Cookie: token=your-jwt-token"Security Features
- Password Hashing: Uses bcryptjs for secure password storage
- JWT Tokens: Secure authentication tokens
- HTTP-only Cookies: Prevents XSS attacks
- CORS Protection: Configurable cross-origin requests
- Input Validation: Server-side validation
- Error Handling: Secure error messages
Environment Variables
| Variable | Description | Default |
|----------|-------------|---------|
| MONGODB_URI | MongoDB connection string | Required |
| JWT_SECRET | JWT signing secret | Required |
| PORT | Server port | 5000 |
| NODE_ENV | Environment mode | development |
| FRONTEND_URL | Frontend URL for CORS | http://localhost:3000 |
Dependencies
- express: Web framework
- mongoose: MongoDB ODM
- bcryptjs: Password hashing
- jsonwebtoken: JWT token handling
- cookie-parser: Cookie parsing middleware
- cors: Cross-origin resource sharing
- dotenv: Environment variable loading
Development
# Install dependencies
npm install
# Start development server with nodemon
npm run dev
# Start production server
npm start🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Express.js team for the amazing framework
- MongoDB team for the database
- All contributors who help improve this template
📨 Connect with me 🦄
Made with ❤️ for the developer community
