How sapphirepegasus/mern-hotelbooking works
Project type and purpose
• This is a MERN-stack full-stack web application for hotel booking (React frontend, Express/Node backend, MongoDB database).
• The README confirms: “Please run the backend first and then the frontend” and mentions a MongoDB token.
File structure
• Top-level “api” folder: Express server, MVC pattern (models, controllers, routes), utility middleware (error handling, token verification), package files.
• Top-level “client” folder: React app (components, pages, hooks, context for Auth and Search, public static assets).
• No other services or microservices—monolith back end and single-page front end.
Main components
a. Client (Browser)
• React SPA
• Pages: Home, List, Hotel, Login, Signup, Reserve
• Components: Header, Footer, Featured, SearchItem, etc.
• Context providers: AuthContext, SearchContext
• Custom hook: useFetch for API calls
b. API Server
• Express app (api/index.js)
• Routes layer: /auth, /hotels, /rooms, /users
• Controllers: business logic for each route
• Models: Mongoose schemas for User, Hotel, Room
• Middleware: verifyToken, error handler
c. Database
• MongoDB Atlas (external service, connection via .env token)
d. External/third-party
• JWT for authentication
• Mongoose ORM
• Axios or fetch (client-side)
Relationships and data flows
• Browser ↔ API over HTTPS/REST (CRUD calls: GET hotels, POST login, etc.)
• API ↔ MongoDB via Mongoose driver
• Frontend Context (AuthContext) wraps protected pages, passes JWT in Authorization header
• useFetch abstracts calls to endpoints
Architectural patterns and design principles
• Client: component-based hierarchy, single-page application, unidirectional data flow (React + Context)
• Server: MVC (Model-View-Controller) with routes → controllers → models
• Secured routes via token verification middleware
• Separation of concerns: UI vs API vs Data layer
Detailed elements
• Component names (e.g., Featured, SearchItem) only if needed for clarity
• Token verification middleware in the server layer preceding protected routes
• DB connection string pulled from .env (annotation)
• Error handler middleware catching controller errors
Capturing essential architecture
• Don’t clutter with every CSS file—focus on logical components.
• How authentication flows from Login page → AuthContext → API/auth → JWT → subsequent requests.
• REST endpoints and their mapping to controllers/models.