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.
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)
# 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! π
# 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# Copy the environment template
cp env.example .envUpdate your .env file with the following:
# Database Configuration
MONGODB_URI=mongodb://localhost:27017/your-database-name
# For MongoDB Atlas: mongodb+srv://username:password@cluster.mongodb.net/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=5dOption 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
# Development mode (with auto-restart)
npm run dev
# Production mode
npm startThe server will start on http://localhost:5000 (or your specified PORT).
- β 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
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 file
| 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 | - |
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Server health check |
| GET | /api/health |
Server health check |
{
"success": true,
"message": "Operation completed successfully",
"data": {
"user": {
"_id": "user_id",
"name": "User Name",
"email": "user@example.com",
"isActive": true,
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
}
}{
"success": false,
"message": "Error description"
}curl -X POST http://localhost:5000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}'curl -X POST http://localhost:5000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "password123"
}'curl -X GET http://localhost:5000/api/auth/logout \
-H "Cookie: token=your-jwt-token"- 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
| 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 |
- 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
# Install dependencies
npm install
# Start development server with nodemon
npm run dev
# Start production server
npm start- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- Express.js team for the amazing framework
- MongoDB team for the database
- All contributors who help improve this template
Made with β€οΈ for the developer community