A full-stack blog application powered by MongoDB with authentication, built using Node.js, Express, React, and TailwindCSS. The application allows users to view blog posts, add comments, and interact with the blog in real-time.
- User authentication (Login and Register).
- Add, view, and delete blog posts.
- Add, view, and manage comments on blog posts.
- Blog posts and comments are displayed in chronological order.
- Instant update of comments and blog posts without page reloads.
- Success and error messages displayed after submitting a comment.
- Hosted on Render for seamless deployment.
- Frontend: React, TailwindCSS, Axios
- Backend: Node.js, Express
- Database: MongoDB
- Authentication: JWT (JSON Web Tokens)
- Hosting: Render (for both frontend and backend)
- Environment Management:
.envfor backend configurations
- Node.js (version >= 14)
- MongoDB (either local or Atlas)
- A Render account for deployment
- JWT secret for authentication
- Clone the repository:
git clone <repo_url>
- Navigate to the backend directory:
cd backend - Install dependencies:
npm install
- Create a .env file in the root directory and set the necessary environment variables like:
PORT=5000 MONGO_URI=<your_mongodb_connection_string> JWT_SECRET=<your_jwt_secret>
- Run the backend server locally:
npm start
The backend will be running on http://localhost:5000.
- Clone the repository if not already done:
git clone <repo_url>
- Navigate to the frontend directory:
cd frontend - Install dependencies:
npm i
- Run the frontend server locally:
npm start or npm run dev
The frontend will be running on http://localhost:5174.
- Sign up or log in to Render.
- Create a new Web Service and connect it to the backend repository.
- Set the environment variables in the Render dashboard:
MONGO_URI: Your MongoDB connection string.JWT_SECRET: Your JWT secret. - Deploy the backend and get the deployed URL (e.g., https://your-backend.onrender.com).
- Deploy Frontend on Render
- Create a new Static Site on Render and connect it to the frontend repository.
- Set the build command and publish directory as:
Build Command:
npm run buildPublish Directory:build/Deploy the frontend and get the deployed URL (e.g., https://your-frontend.onrender.com).
- Ensure both the backend and frontend are deployed or running locally.
- Open the frontend application in the browser (e.g., http://localhost:5174).
- Users can log in or register to add blog posts and comments.
- Blog posts are displayed with the ability to add and manage comments.
- Comments are displayed in chronological order and updated instantly.
- Error and success messages are displayed after submitting a comment.
POST /api/auth/register: Registers a new user.
Body: { firstName, lastName, email, password }
POST /api/auth/login: Logs in a user and returns a JWT token.
Body: { email, password }
GET /api/blogs: Fetches all blog posts.
POST /api/blogs: Adds a new blog post.
Body: { title, content }
Headers: Authorization: Bearer <token>
PUT /api/users/:userId/blogs/:blogId/comment: Adds a new comment to a blog post.
Body: { commentText }
Headers: Authorization: Bearer <token>
src/components/AddComment.js: Contains the logic for adding a comment.
src/components/CommentCard.js: Displays the blog comments.
src/utils/api.js: Contains the API call functions using Axios.
src/pages/Blog.js: The blog page where comments are displayed and added.
controllers/blogController.js: Contains logic for handling blog posts and comments.
controllers/authController.js: Handles user authentication (login and register).
models/Blog.js: MongoDB schema for blog posts.
models/User.js: MongoDB schema for users.
routes/blogRoutes.js: Routes for handling blog post and comment API calls.
routes/authRoutes.js: Routes for authentication (login, register).
You can see the live demo at https://blogger-gefd.onrender.com/
Feel free to use!