How am17jx/book_review works
Project Type and Purpose
• This is a single-service, monolithic REST API for managing books, users, comments, and tags.
• Built on Node.js/Express and backed by a PostgreSQL database.
• Provides JWT authentication, role-based access control, and robust error handling.
File Structure
• Top-level entry:
• server.js – loads environment, starts HTTP server
• app.js – configures Express app, global middleware, and error handlers
• config/ – database connection (config/DataBase.js)
• controllers/ – business logic for each resource (books, users, comments, tags, auth)
• routes/ – Express routers mapping HTTP endpoints to controller functions
• utils/ – AppError (custom error class) and catchAsync (async wrapper)
• package.json – dependencies (Express, pg, JWT, bcrypt, helmet, rate-limit, xss-clean)
README for Insights
• Core domains: Users, Books, Comments, Tags
• Security stack: JWT auth, bcrypt hashing, helmet, rate limiting, XSS sanitization
• DB schema: users, books, comments, tags, book_tag junction table
• Access rules: owner-only updates, admin-only deletes, protected routes
Main Components
a. Clients
• Web or Mobile clients (Postman, front-end apps)
b. API Layer
• Express App (app.js)
• Global middleware: helmet, rate-limit, xss-clean, JSON body parser
• Auth middleware (JWT verification, role checks)
c. Routing Layer
• /api/users, /api/books, /api/comment, /api/tag routers
d. Controller Layer (controllers/*.js)
• authController, userController, bookController, commentController, tagController
• Use catchAsync to wrap async functions, throw AppError on failures
e. Data Access / Model Layer
• config/DataBase.js → pg client or pool connecting to PostgreSQL
• Direct SQL queries or ORM calls (if any)
f. Database
• PostgreSQL instance with five tables: users, books, comments, tags, book_tag
g. Utilities
• AppError for standardized error responses
• catchAsync for error propagation
h. External Libraries/Services
• jsonwebtoken (JWT generation/validation)
• bcryptjs (password hashing)
• dotenv (env vars)
Relationships & Data Flows
• Client → HTTP Request → Express Middleware → Router → Controller → Database → Controller → JSON Response → Client
• Authenticated routes use JWT middleware before routing.
• Role-checks (owner vs. admin) happen in controller or middleware immediately after auth.
• Controllers call DB via DataBase.js; results flow back upstream.
• Errors thrown by controllers propagate through Express’s centralized error handler.
Architectural Patterns & Principles
• Layered (Controller → Data Access → Database)
• Modular (“separation of concerns” for routes, controllers, config, utils)
• Middleware pipeline for cross-cutting concerns (security headers, rate limiting, XSS)
• Centralized error handling with a custom error class and async wrapper