This repository showcases a full-stack web application built with the MERN stack, which includes:
- MongoDB: For database management.
- Express.js: As the server-side framework.
- React.js: For building a responsive and dynamic front-end.
- Node.js: For backend runtime.
Additionally, it leverages React Query to manage server state efficiently and improve data fetching and caching.
- User authentication and authorization.
- CRUD operations.
- Efficient server-state management using React Query.
- Responsive design with React.
- RESTful APIs for seamless client-server communication.
- Scalable and maintainable project structure.
- Node.js and npm installed.
- MongoDB installed locally or an active MongoDB Atlas cluster.
-
Clone the repository:
git clone https://github.com/Ashhad1200/MERN.git cd MERN
-
Install dependencies for both backend and frontend:
cd backend npm install cd ../frontend npm install
-
Configure environment variables:
- Create a
.env
file in thebackend
folder. - Add the following variables:
MONGO_URI=<your-mongodb-uri> JWT_SECRET=<your-jwt-secret> PORT=<desired-port>
- Create a
-
Run the application:
- Start the backend server:
cd backend npm run start
- Start the frontend development server:
cd frontend npm run dev
- Start the backend server:
-
Access the application: Open http://localhost:3000 in your browser.
- backend/: Contains the server-side code with routes, models, and controllers.
- frontend/: Contains React components, hooks, React Query integration, and styling for the client-side.
- Frontend: React, React Query, Axios
- Backend: Node.js, Express
- Database: MongoDB
- Authentication: JSON Web Tokens (JWT)
- Styling: [Mention your styling tools if used, e.g., Bootstrap, TailwindCSS]
The project integrates React Query for:
- Data fetching with automatic caching.
- Server state synchronization.
- Smooth UI updates with background refetching.
Key hooks used:
useQuery
for fetching data.useMutation
for submitting data changes.
import { useQuery } from 'react-query';
import axios from 'axios';
const fetchData = async () => {
const { data } = await axios.get('/api/resource');
return data;
};
const MyComponent = () => {
const { data, isLoading, error } = useQuery('resourceKey', fetchData);
if (isLoading) return <p>Loading...</p>;
if (error) return <p>Error fetching data</p>;
return <div>{JSON.stringify(data)}</div>;
};
Contributions are welcome! To contribute:
- Fork this repository.
- Create a feature branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m 'Add feature-name'
. - Push to the branch:
git push origin feature-name
. - Open a pull request.
This project is licensed under the MIT License.