Skip to content

benchvue/mongodb-express-rest-api-example

Repository files navigation

MongoDB and Express.js REST API sample application

This repository contains the sample application for the MongoDB and Express.js REST API tutorial.

How To Run

  1. 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/.env to your Connection String:
ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
  1. Start the Express server:
cd server
npm install
npm run dev
  1. Start the React app (in a new terminal window):
cd app
npm install
npm start

Disclaimer

Use at your own risk; not a supported MongoDB product

Local Test

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

MongoDB + Express REST API + React (Kubernetes & Docker)

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-server locally/in-cluster (no Atlas needed). Client calls /api and is proxied by nginx to the API service in K8s (no CORS hassles).


Prerequisites

  • Docker Desktop (with Kubernetes enabled) or any K8s cluster
  • kubectl in 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 exec with absolute Linux paths, prefix commands with MSYS_NO_PATHCONV=1 to avoid path mangling.


Repo layout

Build & Push Images

Server image

from repo root

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

Kubernetes Deploy

kubectl apply -f k8s.yaml
kubectl -n mongodb-example get pods

Access

Client (React/nginx): http://localhost:30080/

API (optional direct): http://localhost:30050/

MongoDB inside cluster: mongodb://mongodb:27017/myFirstDatabase

Smoke tests

# 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 .

Optional: Docker Compose (local, no K8s)

docker compose up -d
# open http://localhost:3000 (client)
# API http://localhost:5050
# MongoDB mongodb://localhost:27017

Updating code & rolling out

# 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

About

Demo for MongoDB reset server & client with Docker Compose and Kubernetes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •