How yogeshawasthi/blog_backend_in_mern works
Project type and purpose
• This is the backend of a MERN-stack blog application. Its sole responsibility is to expose RESTful endpoints for authentication, user management, and blog operations, backed by MongoDB.
File structure
• Index.js: server entry point
• config/db.js: database connection logic
• routes/*.js: Express router definitions for auth, blog, and user endpoints
• controllers/*Controller.js: business logic for each route group
• models/*.js: Mongoose schemas for User and Blog
• middlewares/authMiddlewares.js: JWT-based request authentication/authorization
• utils/jwt.js: token creation and verification helper
a. Client (React or any HTTP client)
b. Express API server
• Routing layer
• Authentication middleware
• Controllers (Auth, User, Blog)
c. MongoDB database (via Mongoose models)
d. JWT utility service (token issuance and validation)
Relationships and data flows
• Client issues HTTP requests (GET/POST/PUT/DELETE) → Express routing layer
• Requests pass through auth middleware when needed → Controllers handle business rules → Controllers invoke Mongoose models to read/write MongoDB → Responses bubble back to client
• AuthController uses utils/jwt to sign and verify tokens
• config/db.js establishes and maintains the MongoDB connection
Architectural patterns and design principles
• Layered architecture (Routes → Middleware → Controllers → Data Access)
• Separation of concerns: authentication logic isolated in middleware and utils, business logic in controllers, persistence in models
• Stateless JWT authentication
Technologies and frameworks
• Node.js, Express.js
• MongoDB with Mongoose ODM
• JSON Web Tokens (jwt)
• dotenv or custom config for environment variables (in config/db.js)
Detailed breakdown
• Client layer
• React App → “HTTP requests with JWT in Authorization header”
• API Server layer
1. Index.js starts the server and loads db.js
2. Routes (authRoutes, userRoutes, blogRoutes) dispatch to:
a. Authentication middleware (protects routes)
b. Controllers (authControllers, userController, blogController)
3. Controllers call Mongoose models (user.js, blog.js)
• Persistence layer
• MongoDB database (collections: users, blogs)
• Utility service
• jwt.js for sign/verify operations