# Hotel Booking Server - Setup Guide ## ๐Ÿš€ Quick Start ### 1. Install Dependencies ```bash cd server npm install ``` ### 2. Configure Environment Copy `.env.example` to `.env` and update values: ```bash cp .env.example .env ``` Edit `.env`: ```bash DB_NAME=hotel_db DB_USER=root DB_PASS=your_password JWT_SECRET=your-secret-key ``` ### 3. Setup Database **Option A: Using existing MySQL database** ```bash # Create database mysql -u root -p CREATE DATABASE hotel_db; exit; # Run migrations npm run migrate # (Optional) Seed data npm run seed ``` **Option B: Database will be created automatically** - Just run the server - Make sure MySQL is running - Database will be created on first connection ### 4. Start Server ```bash # Development mode with nodemon npm run dev # Production mode npm start ``` Server will be available at: `http://localhost:3000` ## ๐Ÿ“ก API Endpoints ### Health Check ```bash GET http://localhost:3000/health ``` ### Authentication ```bash POST /api/auth/register POST /api/auth/login POST /api/auth/refresh-token POST /api/auth/logout GET /api/auth/profile (Protected) ``` ## ๐Ÿงช Test API ### Register New User ```bash curl -X POST http://localhost:3000/api/auth/register \ -H "Content-Type: application/json" \ -d '{ "name": "John Doe", "email": "john@example.com", "password": "Password123", "phone": "0123456789" }' ``` ### Login ```bash curl -X POST http://localhost:3000/api/auth/login \ -H "Content-Type: application/json" \ -d '{ "email": "john@example.com", "password": "Password123" }' ``` ## โœ… Server Features - โœ… Express.js setup with security middleware - โœ… JWT authentication (access + refresh tokens) - โœ… Password hashing with bcrypt - โœ… Input validation with express-validator - โœ… Error handling middleware - โœ… Rate limiting - โœ… CORS configuration - โœ… Request logging with Morgan - โœ… Compression middleware - โœ… Helmet security headers ## ๐Ÿ“ Project Structure ``` server/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ config/ โ”‚ โ”‚ โ””โ”€โ”€ database.js # Database configuration โ”‚ โ”œโ”€โ”€ controllers/ โ”‚ โ”‚ โ””โ”€โ”€ authController.js # Auth logic โ”‚ โ”œโ”€โ”€ databases/ โ”‚ โ”‚ โ”œโ”€โ”€ migrations/ # Database migrations โ”‚ โ”‚ โ”œโ”€โ”€ models/ # Sequelize models โ”‚ โ”‚ โ””โ”€โ”€ seeders/ # Seed data โ”‚ โ”œโ”€โ”€ middlewares/ โ”‚ โ”‚ โ”œโ”€โ”€ auth.js # JWT verification โ”‚ โ”‚ โ”œโ”€โ”€ errorHandler.js # Global error handler โ”‚ โ”‚ โ””โ”€โ”€ validate.js # Validation middleware โ”‚ โ”œโ”€โ”€ routes/ โ”‚ โ”‚ โ”œโ”€โ”€ authRoutes.js # Auth routes โ”‚ โ”‚ โ”œโ”€โ”€ userRoutes.js # User routes โ”‚ โ”‚ โ”œโ”€โ”€ roomRoutes.js # Room routes โ”‚ โ”‚ โ””โ”€โ”€ bookingRoutes.js # Booking routes โ”‚ โ”œโ”€โ”€ validators/ โ”‚ โ”‚ โ””โ”€โ”€ authValidator.js # Auth validation rules โ”‚ โ”œโ”€โ”€ app.js # Express app setup โ”‚ โ””โ”€โ”€ server.js # Server entry point โ”œโ”€โ”€ .env # Environment variables โ”œโ”€โ”€ .env.example # Environment template โ””โ”€โ”€ package.json ``` ## ๐Ÿ”ง Troubleshooting ### Database Connection Error ``` Error: Access denied for user 'root'@'localhost' ``` **Solution:** Check DB_USER and DB_PASS in .env ### Port Already in Use ``` Error: listen EADDRINUSE: address already in use :::3000 ``` **Solution:** Change PORT in .env or kill process using port 3000 ### JWT Secret Warning ``` Warning: Using default JWT secret ``` **Solution:** Set JWT_SECRET in .env to a strong random string ## ๐Ÿ“ Notes - Default customer role_id = 3 - Access token expires in 1 hour - Refresh token expires in 7 days (or 1 day without "Remember Me") - Password must contain uppercase, lowercase, and number - Password minimum length: 8 characters