A modern React blog application with AI-powered content generation using OpenAI's GPT-4o-mini model.
- 📝 Create, view, and manage blog posts
- 🤖 AI-powered blog content generation using OpenAI
- 🎨 Clean, responsive UI with Bootstrap
- 🔄 Real-time data with JSON Server
- ⚡ Fast development with Vite
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- npm (comes with Node.js)
- An OpenAI API key
git clone <your-repo-url>
cd react-ai-blognpm installnpm install -g json-serverCreate a .env file in the project root directory:
# Windows (PowerShell)
New-Item -Path .env -ItemType File
# Or manually create .env fileAdd your OpenAI API key to the .env file:
VITE_OPENAI_API_KEY=sk-your-openai-api-key-here
⚠️ Important: Never commit your.envfile to version control. It should already be in.gitignore.
- Go to OpenAI Platform
- Sign in or create an account
- Navigate to API Keys section
- Click Create new secret key
- Copy the key and paste it in your
.envfile
You need to run two terminals - one for the JSON Server (backend) and one for the React app (frontend).
json-server --watch data/db.json --port 3001This starts the mock API server at http://localhost:3001
npm run devThis starts the Vite dev server at http://localhost:5173
- Open your browser and go to
http://localhost:5173 - Browse existing blog posts on the home page
- Click Create Blog to add a new post
- Enter a blog title
- Click "Generate Blog Body using AI" to auto-generate content using OpenAI
- Select an author and submit
react-ai-blog/
├── data/
│ └── db.json # JSON Server database
├── src/
│ ├── pages/
│ │ ├── About.jsx # About page
│ │ ├── BlogDetails.jsx # Single blog view
│ │ ├── Blogs.jsx # Blog list component
│ │ ├── CreateBlog.jsx # Create blog with AI generation
│ │ ├── Footer.jsx # Footer component
│ │ ├── Header.jsx # Navigation header
│ │ ├── Home.jsx # Home page
│ │ ├── NotFound.jsx # 404 page
│ │ └── useFetch.js # Custom fetch hook
│ ├── App.jsx # Main app component
│ ├── App.css # App styles
│ ├── index.css # Global styles
│ └── main.jsx # Entry point
├── .env # Environment variables (create this)
├── .gitignore
├── package.json
├── vite.config.js
└── README.md
| Method | Endpoint | Description |
|---|---|---|
| GET | /blogs | Get all blogs |
| GET | /blogs/:id | Get a single blog |
| POST | /blogs | Create a new blog |
| DELETE | /blogs/:id | Delete a blog |
| Command | Description |
|---|---|
npm run dev |
Start development server |
npm run build |
Build for production |
npm run preview |
Preview production build |
npm run lint |
Run ESLint |
- Frontend: React 19, React Router 7
- Build Tool: Vite 7
- AI: OpenAI API (GPT-4o-mini)
- Mock Backend: JSON Server
- Styling: Bootstrap 5
Make sure your .env file exists and contains VITE_OPENAI_API_KEY. Restart the Vite server after creating/modifying the .env file.
Install it globally: npm install -g json-server
- Change JSON Server port:
json-server --watch data/db.json --port 3002 - Update the fetch URLs in your components accordingly
MIT