This repository contains the sample application for the MongoDB and Express.js REST API tutorial.
- You can follow the Getting Started with Atlas guide, to learn how to create a free Atlas account, create your first cluster and get your Connection String to the database.
Then, set the Atlas URI connection parameter in
server/.envto your Connection String:
ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
- Start the Express server:
cd server
npm install
npm run dev
- Start the React app (in a new terminal window):
cd app
npm install
npm start
Use at your own risk; not a supported MongoDB product
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest
docker build -t mongodb-server:latest ./server
set NODE_OPTIONS=--openssl-legacy-provider && npm start
docker build -t mongodb-client-app:latest ./app
docker tag mongodb-client-app:latest benchvue/mongodb-client-app:latest docker push benchvue/mongodb-client-app:latest
docker tag mongodb-server:latest benchvue/mongodb-server:latest docker push benchvue/mongodb-server:latest
Production-ready split of the MongoDB Developer example into:
- server/ – Node/Express REST API
- app/ – React client (built with CRA, served by nginx)
- k8s.yaml – one-file Kubernetes deployment (MongoDB, API, Client)
- docker-compose.yml – optional local compose (MongoDB, API, Client)
Uses
mongodb/mongodb-community-serverlocally/in-cluster (no Atlas needed). Client calls/apiand is proxied by nginx to the API service in K8s (no CORS hassles).
- Docker Desktop (with Kubernetes enabled) or any K8s cluster
kubectlin your PATH- Docker Hub account (images are tagged as
benchvue/*in examples) - Node 18+ if you run locally (we build inside containers, so optional)
Windows + Git Bash tip: when using
kubectl execwith absolute Linux paths, prefix commands withMSYS_NO_PATHCONV=1to avoid path mangling.
Server image
docker build -t benchvue/mongodb-server:latest ./server
docker push benchvue/mongodb-server:latest
Client image
docker build \
--build-arg REACT_APP_API_URL=/api \
-t benchvue/mongodb-client-app:latest ./app
docker push benchvue/mongodb-client-app:latest
kubectl apply -f k8s.yaml
kubectl -n mongodb-example get pods
Client (React/nginx): http://localhost:30080/
API (optional direct): http://localhost:30050/
MongoDB inside cluster: mongodb://mongodb:27017/myFirstDatabase
# Should return 200 OK with [] initially
curl -i http://localhost:30080/api/posts
# Create a sample post through the proxy
curl -i -X POST http://localhost:30080/api/posts \
-H 'Content-Type: application/json' \
-d '{"author":"Bench","title":"Hello","tags":["k8s","mongo"],"body":"It works!"}'
# List again
curl -s http://localhost:30080/api/posts | jq .
docker compose up -d
# open http://localhost:3000 (client)
# API http://localhost:5050
# MongoDB mongodb://localhost:27017
# Rebuild with a NEW TAG to avoid :latest caching
docker build --build-arg REACT_APP_API_URL=/api -t benchvue/mongodb-client-app:v1 ./app
docker push benchvue/mongodb-client-app:v1
kubectl -n mongodb-example set image deploy/mongodb-client-app client=benchvue/mongodb-client-app:v1
kubectl -n mongodb-example rollout status deploy/mongodb-client-app
# Server example
docker build -t benchvue/mongodb-server:v1 ./server
docker push benchvue/mongodb-server:v1
kubectl -n mongodb-example set image deploy/mongodb-server server=benchvue/mongodb-server:v1
kubectl -n mongodb-example rollout status deploy/mongodb-server