A small full-stack app that sends your code snippet to Google Gemini and shows a plain-English explanation. The UI is a React SPA; the AI call runs on an Express API so your API key stays on the server.
| Layer | Technologies |
|---|---|
| Frontend | React 19, Vite 5, Tailwind CSS v4, react-markdown + remark-gfm |
| Backend | Express 5, Google Gen AI SDK (@google/genai), CORS, Helmet, express-rate-limit, dotenv |
- You choose a language, paste code, and submit the form.
- The browser runs a server action (
explainCode) thatPOSTs{ code, language }as JSON to the Express routePOST /api/code-explanation. - The server builds a prompt, calls Gemini (
generateContent), and returns JSON{ explanation, language }. - The client shows the explanation in a card below the form; markdown from the model is rendered with react-markdown (GitHub-flavored markdown via remark-gfm).
- If the model is overloaded (e.g. HTTP 503 /
UNAVAILABLE), the server maps Gemini’s error into a clearerrormessage; the client shows it in the error panel instead of a generic failure.
Form fields use controlled state so your code and language stay visible after submit. The form and explanation share one max-width container (max-w-5xl) so layout stays aligned on wide screens.
react-express-ai/
├── code-explanation/ # Vite + React frontend
├── server/ # Express API + Gemini
├── docs/
│ └── screenshot.png # UI screenshot (used in this README)
└── README.md
- Node.js (LTS recommended)
- A Google AI (Gemini) API key with access to the configured model
cd server
npm installCreate a .env file in server/ (do not commit real keys):
GEMINI_API_KEY=your_gemini_api_key_here
PORT=3001
FRONTEND_URL=http://localhost:5173PORT: API port (default in code is3002if omitted; use one value and keep the frontend URL in sync).FRONTEND_URL: Origin allowed by CORS (Vite defaults to port 5173).
Start the API:
npm run devcd code-explanation
npm installCreate .env in code-explanation/:
VITE_API_BASE_URL=http://localhost:3001/apiUse the same host/port as PORT in the server, with /api as the path prefix (the client calls ${VITE_API_BASE_URL}/code-explanation).
Start the dev server:
npm run devOpen the printed local URL (usually http://localhost:5173), submit code, and check the explanation below the form.
| Command | Purpose |
|---|---|
npm run dev |
Vite dev server with HMR |
npm run build |
Production build to dist/ |
npm run preview |
Preview production build |
npm run lint |
ESLint |
- Language dropdown and code textarea with named fields for correct
FormData/ JSON submission. - Controlled inputs so text remains after submit.
- Shared width for form + explanation; wide layout (
max-w-5xl). - API error handling: user-visible messages for Gemini overload and other upstream errors.
- Markdown explanations with GFM support (lists, tables, etc., per remark-gfm).
- code-explanation/README.md — frontend-only notes.
- server/README.md — API and environment variables.
