A professional, production-ready Express.js REST API with JWT authentication, MySQL database, and comprehensive error handling.
- Authentication & Authorization: JWT-based authentication system
- Database: MySQL with Sequelize ORM
- Security: Helmet, CORS, Rate limiting
- Validation: Request validation with Joi
- File Upload: Multer for handling file uploads
- Error Handling: Centralized error handling
- Logging: Morgan for HTTP request logging
- Testing: Jest testing framework
- Code Structure: Clean architecture with services, controllers, middleware
src/
βββ config/ # Database and application configuration
βββ controllers/ # Request handlers
βββ middleware/ # Custom middleware functions
βββ models/ # Database models (Sequelize)
βββ routes/ # API routes
βββ services/ # Business logic layer
βββ utils/ # Utility functions and helpers
βββ app.js # Express application setup
βββ server.js # Server entry point
public/
βββ uploads/ # File upload directory
tests/ # Test files
-
Clone the repository
git clone <repository-url> cd whatsapp-gateway-backend
-
Install dependencies
npm install
-
Environment Setup
cp .env.example .env
Update the
.env
file with your configuration:PORT=3001 NODE_ENV=development DB_HOST=localhost DB_PORT=3306 DB_NAME=backend_starter DB_USERNAME=root DB_PASSWORD=your_password TOKEN_SECRET=your_jwt_secret_key
-
Database Setup
- Create a MySQL database named
backend_starter
- The application will automatically sync the database schema
- Create a MySQL database named
-
Start the application
# Development npm run dev # Production npm start
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}
POST /api/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}
POST /api/auth/refresh
Authorization: Bearer <token>
GET /api/users/profile
Authorization: Bearer <token>
PUT /api/users/profile
Authorization: Bearer <token>
Content-Type: multipart/form-data
{
"name": "John Updated",
"email": "john.updated@example.com",
"phone": "1234567890",
"picture": <file>
}
GET /api/todos?page=1&limit=10&status=pending
Authorization: Bearer <token>
POST /api/todos
Authorization: Bearer <token>
Content-Type: application/json
{
"title": "Complete project",
"description": "Finish the backend API development",
"deadline": "2024-12-31T23:59:59.000Z",
"status": "pending"
}
GET /api/todos/:id
Authorization: Bearer <token>
PUT /api/todos/:id
Authorization: Bearer <token>
Content-Type: application/json
{
"title": "Updated title",
"status": "completed"
}
DELETE /api/todos/:id
Authorization: Bearer <token>
GET /api/todos/stats
Authorization: Bearer <token>
GET /api/example/health
GET /api/example/test
Authorization: Bearer <token>
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
- Use ES6+ features
- Follow consistent naming conventions
- Write descriptive commit messages
- Add JSDoc comments for functions
# Generate migration
npx sequelize-cli migration:generate --name migration-name
# Run migrations
npx sequelize-cli db:migrate
# Undo last migration
npx sequelize-cli db:migrate:undo
NODE_ENV=production
PORT=3001
DB_HOST=your_production_db_host
DB_NAME=your_production_db_name
DB_USERNAME=your_production_db_user
DB_PASSWORD=your_production_db_password
TOKEN_SECRET=your_strong_jwt_secret
CORS_ORIGIN=https://yourdomain.com
# Install PM2
npm install -g pm2
# Start application
pm2 start src/server.js --name "backend-api"
# Monitor
pm2 monit
# Logs
pm2 logs backend-api
The API uses consistent error response format:
{
"success": false,
"message": "Error description",
"timestamp": "2024-01-01T00:00:00.000Z"
}
- JWT Authentication: Secure token-based authentication
- Password Hashing: bcrypt with salt rounds
- Rate Limiting: Prevents API abuse
- CORS: Configurable cross-origin resource sharing
- Helmet: Security headers
- Input Validation: Joi schema validation
- 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 ISC License.
- Fajri Rinaldi Chan - Initial work
- Express.js community
- Sequelize documentation
- JWT.io for token handling
- All contributors and testers