A robust, production-ready Express.js starter template with MySQL integration using Prisma ORM, featuring comprehensive user authentication, email functionality, and file upload capabilities.
-
π User Authentication
- JWT-based secure authentication
- Role-based authorization (User/Admin)
- Protected routes with middleware
- Session management with cookies
-
π§ Email Integration
- Gmail SMTP integration
- Beautiful HTML email templates
- Secure password reset workflow
- Transactional email support
-
π File Management
- User avatar uploads
- Secure file storage
- Automatic file cleanup
- Default avatar support
-
π‘οΈ Security
- Role-based access control (RBAC)
- Input validation
- Password hashing with bcrypt
- Secure reset code generation
-
πΎ Database
- MySQL with Prisma ORM
- Automated migrations
- Type-safe database queries
- Efficient connection pooling
-
βοΈ Development Tools
- Jest testing setup
- Environment configuration
- API error handling
- Standardized response format
βββ config/ # Configuration files
βββ controller/
β βββ authController.js # Authentication logic
β βββ userController.js # User management
β βββ forgetPasswordController.js
βββ Middleware/
β βββ authMiddleware.js # JWT authentication
β βββ validationMiddleware.js
βββ prisma/
β βββ schema.prisma # Database schema
β βββ migrations/ # Database migrations
βββ router/
β βββ authRouter.js
β βββ userRouter.js
β βββ forgetPasswordRouter.js
βββ utils/
β βββ APIError.js # Error handling
β βββ APIResponse.js # Response formatting
β βββ sendMail.js # Email utility
βββ uploads/
β βββ userAvatar/ # User avatar storage
βββ tests/ # Jest test files
- Node.js (v14 or higher)
- MySQL Server (v5.7 or higher)
- npm or yarn
git clone <https://github.com/mrXrobot26/ExpreesTemplateWithSQL>
cd ExpreesTemplateWithSQL
npm install
Create a .env
file in the root directory:
# Application
NODE_ENV=development
PORT=3000
# Database
DATABASE_URL="mysql://username:password@localhost:3306/your_database"
# Authentication
JWT_SECRET=your_jwt_secret_key
# Email Configuration (Gmail)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your_email@gmail.com
EMAIL_PASSWORD=your_app_specific_password
# Create database tables
npx prisma migrate dev
npm run dev
Your API will be available at http://localhost:3000/api/v1
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /api/v1/auth/register |
Register a new user | No |
POST | /api/v1/auth/login |
User login | No |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /api/v1/forget-password |
Request password reset | No |
POST | /api/v1/forget-password/verify-code |
Verify reset code | No |
POST | /api/v1/forget-password/reset-password |
Set new password | No |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /api/v1/users |
Get all users | Admin |
GET | /api/v1/users/:id |
Get user by ID | Yes* |
PUT | /api/v1/users/:id |
Update user | Yes* |
DELETE | /api/v1/users/:id |
Delete user | Yes* |
PATCH | /api/v1/users/:id/avatar |
Update user avatar | Yes* |
* Users can only access their own resources unless they have admin privileges
-
Client sends POST request to
/api/v1/auth/register
with:{ "name": "John Doe", "email": "john@example.com", "password": "securePassword123", "passwordConfirm": "securePassword123" }
-
Server validates input, hashes password, and creates user
-
Server returns JWT token and user data
-
Client sends POST request to
/api/v1/auth/login
with:{ "email": "john@example.com", "password": "securePassword123" }
-
Server validates credentials and issues JWT token
-
Token is returned in response and set as HTTP-only cookie
-
Request reset code:
POST /api/v1/forget-password { "email": "user@example.com" }
-
Verify reset code:
POST /api/v1/forget-password/verify-code { "email": "user@example.com", "resetCode": "123456" }
-
Set new password:
POST /api/v1/forget-password/reset-password { "email": "user@example.com", "newPassword": "newSecurePassword123" }
- Enable 2-Step Verification in your Google Account
- Generate an App Password:
- Go to Google Account Settings β Security β 2-Step Verification β App passwords
- Select "Mail" and "Other" (name it "Express App")
- Use the 16-character password in your
.env
file
The system includes pre-built HTML email templates for:
- Password reset codes
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
password String
avatar String @default("avatar.png")
passwordResetCode String?
passwordResetExpires DateTime?
passwordResetVerify Boolean @default(false)
}
enum Role {
USER
ADMIN
}
# Run all tests
npm test
# Run specific test suite
npm test -- --testPathPattern=auth
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.