Skip to content

UntaArabia/backend-Express-REST-API

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Backend Starter - Express.js REST API

A professional, production-ready Express.js REST API with JWT authentication, MySQL database, and comprehensive error handling.

πŸš€ Features

  • 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

πŸ“ Project Structure

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

πŸ› οΈ Installation

  1. Clone the repository

    git clone <repository-url>
    cd whatsapp-gateway-backend
  2. Install dependencies

    npm install
  3. 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
  4. Database Setup

    • Create a MySQL database named backend_starter
    • The application will automatically sync the database schema
  5. Start the application

    # Development
    npm run dev
    
    # Production
    npm start

πŸ“š API Documentation

Authentication Endpoints

Register User

POST /api/auth/register
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "password123"
}

Login User

POST /api/auth/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "password123"
}

Refresh Token

POST /api/auth/refresh
Authorization: Bearer <token>

User Endpoints

Get Profile

GET /api/users/profile
Authorization: Bearer <token>

Update Profile

PUT /api/users/profile
Authorization: Bearer <token>
Content-Type: multipart/form-data

{
  "name": "John Updated",
  "email": "john.updated@example.com",
  "phone": "1234567890",
  "picture": <file>
}

Todo Endpoints

Get All Todos

GET /api/todos?page=1&limit=10&status=pending
Authorization: Bearer <token>

Create Todo

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 Todo by ID

GET /api/todos/:id
Authorization: Bearer <token>

Update Todo

PUT /api/todos/:id
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "Updated title",
  "status": "completed"
}

Delete Todo

DELETE /api/todos/:id
Authorization: Bearer <token>

Get Todo Statistics

GET /api/todos/stats
Authorization: Bearer <token>

Example Endpoints

Health Check

GET /api/example/health

Test Endpoint

GET /api/example/test
Authorization: Bearer <token>

πŸ§ͺ Testing

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

πŸ”§ Development

Code Style

  • Use ES6+ features
  • Follow consistent naming conventions
  • Write descriptive commit messages
  • Add JSDoc comments for functions

Database Migrations

# 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

πŸš€ Deployment

Environment Variables for Production

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

PM2 (Recommended for production)

# Install PM2
npm install -g pm2

# Start application
pm2 start src/server.js --name "backend-api"

# Monitor
pm2 monit

# Logs
pm2 logs backend-api

πŸ“ Error Handling

The API uses consistent error response format:

{
  "success": false,
  "message": "Error description",
  "timestamp": "2024-01-01T00:00:00.000Z"
}

πŸ” Security Features

  • 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

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the ISC License.

πŸ‘₯ Authors

  • Fajri Rinaldi Chan - Initial work

πŸ™ Acknowledgments

  • Express.js community
  • Sequelize documentation
  • JWT.io for token handling
  • All contributors and testers

About

User Authentication JWT Rest API Mysql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 87.7%
  • Batchfile 12.3%