How divishtk/authapiwithejs works
Project type and purpose
• From the file tree (Express‐style entry points, EJS views, “models,” “routes,” a Mongo DB connector, and file‐upload utilities), this is a server‐side–rendered full‑stack Node.js web application.
• Purpose: provide user authentication, profile management (with image upload), and post handling via Express + EJS + MongoDB.
File structure
• Top level:
• index.js / app.js – application bootstrap (Express instance, middleware wiring, route mounting)
• DB/mongo.db.js – MongoDB connection logic
• routes/user.routes.js – HTTP endpoints for users, profiles, posts
• models/*.models.js – Mongoose schemas for users and posts
• middlewares/auth.middleware.js – JWT‐based auth guard
• utils/ (db.utils.js, jwt.utils.js) + assets/multer.utils.js – helper modules for DB helpers, JWT creation/validation, file‐upload handling
• public/images/upload – static folder for storing uploaded assets
• views/*.ejs – server‐rendered templates (login, index, profile, edit, etc.)
Core components to reflect
a. Client (Web browser)
b. Web server (Node.js + Express)
• Middleware layer (authentication, file upload)
• Routing layer (user.routes)
• Template engine (EJS)
c. Data layer
• MongoDB instance
• File storage (local FS under public/images/upload)
d. Utility services
• JWT service (signing/verifying tokens)
• Multer service (handling multipart uploads)
Relationships and interactions
• Browser → HTTP → Express routes
• Routes invoke middleware (auth, multer) before controller logic
• Controllers call model methods → MongoDB (via mongo.db.js + db.utils)
• Controllers may read/write files on request (via multer.utils.js)
• Controllers render responses via EJS templates or redirect with JWT tokens set in cookies/headers
Architectural patterns & design principles
• MVC‐inspired separation:
• Models (Mongoose schemas)
• Views (EJS templates)
• Controllers (route handlers)
• Middleware pipeline for cross‐cutting concerns (auth, uploads)
• Utility modules for single responsibilities (DB helper, JWT helper, multer helper)
1. Create a large “Express App” container.
a. Middleware: auth.middleware.js, multer.utils.js
b. Controllers / Routes: user.routes.js → posts/users handlers
c. View Renderer: EJS templates (index.ejs, login.ejs, profile.ejs, etc.)
3. To the right, place “MongoDB” with lines back to models (posts.models.js, users.models.js).
4. Also to the right, place “Local File Storage” pointing from the Multer layer.
5. On the left, place “Web Browser” sending HTTP requests and receiving HTML.
• HTTP transport
• Database access
• File I/O