How saulvo/build-a-storefront-backend-udacity works
Project Type & Purpose
• This is a standalone backend service (Node.js + TypeScript) providing a RESTful storefront API (products, users, orders).
• It uses a PostgreSQL database (via Docker / docker-compose), migrations, and includes automated tests and CI workflows.
File Structure
• Top-level “src/” – source code:
– server.ts: entry point, sets up HTTP server and routes
– handlers/ (controllers): one handler per resource (order.ts, product.ts, user.ts)
– models/ (data layer): ORM or query wrappers for each resource
– helper/ (utilities): auth token logic, shared helpers
– database.ts: DB‐connection configuration (driven by .env + database.json)
– ship/ (API‐spec or CLI‐task definitions)
• migrations/: DB schema evolution scripts (with SQL up/down files)
• tests/: unit/integration specs for handlers & models
• CI/CD: .github/workflows/manual.yml
• Config: .env example, tsconfig.json, package.json, docker-compose.yml
README Insights
• Describes environment setup, Docker for Postgres, migration commands (db-up), build & start scripts, test configuration.
• No frontend – clients interact via HTTP on port 3000.
Main Components
A. API Gateway / HTTP Server
– Node.js runtime (TypeScript) hosting an HTTP server (Express or built-in)
– Reads .env for PORT, DB credentials, TOKEN_SECRET
B. Handlers (Controllers)
– product, user, order handlers implementing REST endpoints (CRUD + login)
C. Business/Data Layer (Models)
– Models define queries against PostgreSQL, assemble domain objects
D. Helpers & Auth
– JWT token generation, request validation
E. Database
– PostgreSQL container (docker-compose)
– Manages storefront_products, storefront_users, storefront_orders, order_products tables
– Migration service to version schema
F. External Interfaces
– HTTP clients (web/mobile apps, Postman)
– CI/CD (GitHub Actions)
– “ship” scripts (CLI or API spec files)
Relationships & Data Flow
1. Client → HTTP Server (port 3000)
2. Server routes request → appropriate Handler
3. Handler invokes Models (through database.ts connection)
4. Models execute SQL → PostgreSQL
5. PostgreSQL returns data → Models → Handler → Response
6. Auth helper intercepts protected routes → verifies TOKEN_SECRET
7. Migrations service updates schema on startup (npm run db-up)
8. CI workflow builds, tests, and can (optionally) deploy
Architectural Patterns
• Layered Architecture (Controller → Service/Model → Data Store)
• Modular organization by domain (order, product, user)
• Configuration via environment and JSON files
• Automated schema migrations (up/down SQL files)
• Test suites organized by layer (handler-specs, model-specs)
Technologies & Frameworks
• Node.js + TypeScript
• PostgreSQL (Docker)
• Migration tool (custom SQL files)
• Testing: Jasmine (spec/support/jasmine.json)
• CI: GitHub Actions (.github/workflows)
• “ship” (custom CLI or API spec integration)