This is a full-stack MERN stack application (MongoDB, Express, React, Node.js) that implements basic CRUD operations (Create, Read, Update, Delete) on records. It is designed to manage and display data in an easy-to-use, responsive web interface.
- Create: Add new records.
- Read: View a list of all records or a single record.
- Update: Edit existing records.
- Delete: Remove records.
- Responsive design using React and Bootstrap for a clean, mobile-friendly UI.
- React is used for building the user interface. React allows for the development of single-page applications (SPA) by updating the DOM efficiently when data changes.
- The app follows a component-based architecture where UI elements are divided into smaller reusable components such as
CreatePost,EditPost,Home, andPostDetails. - React Router is used to manage navigation between pages (Home, Add Post, Edit Post, Post Details).
- Axios is used for making HTTP requests to the backend (CRUD operations).
- The backend is built using Node.js and Express.
- Express provides a minimal and flexible Node.js web application framework for creating API routes.
- The backend handles the logic for CRUD operations and interacts with the MongoDB database using Mongoose.
- The backend exposes RESTful API endpoints to manage records.
- MongoDB is used as the database to store data in JSON-like format.
- The database interacts with the backend via Mongoose to manage the data through schema validation.
- The schema defines the structure of the data (e.g., Post structure with fields:
title,description,category).
The application uses several design patterns to structure and maintain the codebase effectively:
The application follows the MVC design pattern, which separates the application into three interconnected components:
-
Model: Represents the data and business logic of the application. In the backend, this is implemented using Mongoose schemas and models.
- Example:
Post.js(in the backendmodels/directory) defines the structure of the post data.
- Example:
-
View: Represents the user interface (UI). In this application, React components serve as the view that displays the data to the user.
- Example: Components like
Home.js,CreatePost.js,PostDetails.js.
- Example: Components like
-
Controller: Handles the requests and responses. In the backend, Express routes act as controllers, receiving the request, interacting with the models, and returning the appropriate response to the frontend.
- Example: Routes like
/posts,/post/savehandle CRUD operations on the posts.
- Example: Routes like
The backend follows RESTful principles for creating APIs, ensuring that the application uses standardized HTTP methods for CRUD operations:
- GET: Retrieve data (e.g., Get all posts or a single post).
- POST: Create new data (e.g., Add a new post).
- PUT: Update existing data (e.g., Edit a post).
- DELETE: Delete data (e.g., Remove a post).
Each component or module in the application follows the Single Responsibility Principle, where each module (e.g., React component, Express route, etc.) has only one responsibility.
- Example: The
CreatePostcomponent only handles the form for creating new posts, while thePostControllerin the backend is responsible for managing database interactions.
-
User Navigates to Create Post Page:
- User accesses
/addroute. - The
CreatePostcomponent is rendered with a form to input title, description, and category.
- User accesses
-
Form Submission:
- The form sends a
POSTrequest to the backend's/post/saveendpoint using Axios. - The backend receives the data, saves it to MongoDB, and responds with a success message.
- The form sends a
-
Success Handling:
- If the post is saved successfully, the
CreatePostcomponent resets the form and redirects the user to theHomepage using React Router. - If the operation fails, an error message is shown.
- If the post is saved successfully, the
-
Handle Post Request:
- When the frontend sends a request to
/post/save, the backend controller (PostController) receives the request. - The data is validated, and a new
Postis created and saved to the database.
- When the frontend sends a request to
-
Database Interaction:
- Mongoose is used to interact with MongoDB. The post data is saved in the
postscollection. - Mongoose validates the data before saving it to ensure it follows the schema.
- Mongoose is used to interact with MongoDB. The post data is saved in the
-
Response:
- Upon successful save, the backend responds with a success message and the newly created post data.
- If any errors occur, a failure message with details is returned to the frontend.
-
Frontend:
- React.js – For building the user interface.
- React Router – For navigation and routing between pages.
- Axios – For making HTTP requests to the backend.
- Bootstrap – For responsive UI components and styling.
-
Backend:
- Node.js – For the server-side runtime.
- Express.js – For the backend API and routing.
- MongoDB – For storing and managing data.
- Mongoose – For MongoDB object modeling and schema validation.
-
Development Tools:
- Webpack – For bundling the frontend assets.
- Babel – For transpiling modern JavaScript code.
- Postman – For testing API endpoints.
- Git – For version control.
-
POST
/post/save -
Request Body:
{ "title": "New Record", "description": "Record Description", "category": "Category" }