How does the MERN stack handle client-server communication, and how would you secure the API endpoints?
Utilisateur anonyme
What they expect in a good answer: 🔸 1. Communication Flow in MERN: React (frontend) makes HTTP requests using fetch or axios. These requests hit Node.js + Express (backend), which handles routing and logic. Backend interacts with MongoDB using Mongoose for CRUD operations. The backend sends back JSON responses to the React frontend. 🔸 2. Securing API Endpoints: Authentication: Implement JWT-based authentication. On successful login, issue a token signed with a secret key. Store the token in HTTP-only cookies or secure local storage. Authorization: Protect routes using middleware. Example: Admin-only routes, user-specific data routes. Validation: Use libraries like express-validator or Joi to validate incoming request data. Rate Limiting: Use middleware like express-rate-limit to prevent brute force attacks. CORS: Enable and configure properly for cross-origin requests. HTTPS: Always enforce HTTPS in production using proxies or services like Nginx/Cloudflare.