How azure-samples/react-cosmosdb works
Project type and purpose
• This is a full-stack demo application that pairs a React single-page app (SPA) with a Node/Express API server, persisting data in Azure CosmosDB via the MongoDB API.
• Its goal is to demonstrate how to perform CRUD operations from React to CosmosDB, debug both client and server, and deploy/scale on Azure.
File structure
• Top level
• public/ & src/ → Create React App front-end
• server/ → Express back-end, contains app.js, routes/, service & model layers, MongoDB client setup, view templates for errors, and environment config
• Dockerfile, package.json (root), server/package.json → build & runtime configs for both front-end and back-end
• Patterns
• SPA on the client
• On the server: MVC-ish layering (routes → service → model → database)
• Separate dev processes for React dev server and Express in development; unified server in production
README for additional insights
• Stack: React, Node.js/Express, Azure CosmosDB (Mongo API)
• Dev instructions: “node server/server.js” + “npm start” for CRA
• Prod instructions: build React app, then serve static assets from Express on port 3001
• Environment config lives in server/env/environment.js (renamed from environment-change-me.js)
a. Main components
• Client (Browser/React SPA)
• Development servers (React dev server & Express dev server)
• Production server (Express serving static React build)
• HeroService & HeroModel layers in Express
• Azure CosmosDB (Mongo API)
• Environment/config module
• Optional: Docker container wrapping the production server
b. Relationships and interactions
• Browser → HTTP(S) GET/POST/PUT/DELETE → Express API endpoints
• Express routes invoke HeroService → HeroModel → MongoDB client → CosmosDB
• In production: Express also serves React’s static JS/CSS from build/ or public/
• Configuration module injects connection parameters into MongoDB client
c. Architectural patterns & design principles
• Front-end/back-end separation (client-server architecture)
• SPA communicating via a RESTful API
• Server-side layering (routing → business logic → data access)
• Environment-driven config for portability (dev vs prod vs Docker)
d. Key technologies & frameworks
• React (Create React App)
• Node.js, Express, Jade (for error views)
• MongoDB Node.js driver configured for CosmosDB’s Mongo API
• Azure CosmosDB
• Docker (production image in Dockerfile)