A full-stack web application for managing contacts with CRUD operations, built using the MERN stack (MongoDB, Express.js, React.js, Node.js).
- Create, Read, Update, and Delete Contacts: Perform full CRUD operations on contacts.
- Responsive Design: A modern, responsive user interface built with Tailwind CSS.
- Real-Time Form Validation: Instant feedback on user input to ensure data quality.
- Sortable Contact List: Easily sort contacts by name, email, or other attributes.
- Secure API Endpoints: Protected backend routes with proper error handling.
- React.js with Hooks for efficient state management
- Axios for making API calls
- React Table for displaying contact lists
- Tailwind CSS for utility-first, responsive styling
- Node.js with Express.js for building the server
- MongoDB with Mongoose ODM for the database
- Express Validator for input validation
- REST API architecture for structured endpoints
contact-management/
├── client/
│ ├── public/
│ └── src/
│ ├── components/
│ ├── services/
│ ├── App.jsx
│ └── index.js
└── server/
├── config/
├── controllers/
├── models/
├── routes/
└── server.js -
Clone the repository:
git clone [email protected]:sandeshkhairnar/contact-management.git cd contact-management
-
Install dependencies for both the frontend and backend:
npm run install-all
-
Set up environment variables:
-
Create a
.envfile in theserverdirectory:PORT=5000 MONGODB_URI=mongodb://localhost:27017/contact_management -
Create a
.envfile in theclientdirectory:REACT_APP_API_URL=http://localhost:5000/api
-
-
Initialize the database:
-
Start MongoDB service:
mongod
-
Create the database:
mongo use contact_management
-
-
Start the application:
# From the root directory npm startThe application will be available at:
- Frontend: http://localhost:3000
- Backend API: http://localhost:5000
GET /api/contacts- Get all contactsGET /api/contacts/:id- Get a specific contactPOST /api/contacts- Create a new contactPUT /api/contacts/:id- Update an existing contactDELETE /api/contacts/:id- Delete a contact
- Components are organized by feature in the
componentsfolder. - API Calls are centralized in the
servicesdirectory. - Backend routes follow RESTful conventions.
- React Hooks (
useState,useEffect) for managing state. - Axios for handling API requests.
- Tailwind CSS for modern, responsive UI.
- Responsive Design with mobile-first principles.
- Real-time form validation with descriptive error messages.
- Error boundaries and try-catch blocks for reliable UI.
- Input validation using express-validator.
- Centralized error middleware for consistent error responses.
- Robust try-catch blocks for async operations.
Challenge: Initially, running the server and client applications simultaneously required opening multiple terminal windows and running commands separately.
Solution: Implemented the concurrently npm package in the root directory to run both applications with a single command. Modified the root package.json to include the following scripts:
"scripts": {
"start": "concurrently \"npm run server\" \"npm run client\"",
"server": "cd server && npm run start",
"client": "cd client && npm run start"
}Challenge: Encountered issues with database connections timing out during development and unstable connections in the production environment.
Solution: Implemented robust connection handling in server/config/db.js:
const connectDB = async () => {
try {
console.log("Connecting to MongoDB...");
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (err) {
console.error('Error connecting to MongoDB:', err.message);
process.exit(1); // Exit process if connection fails
}
};