<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Devcodes on Medium]]></title>
        <description><![CDATA[Stories by Devcodes on Medium]]></description>
        <link>https://medium.com/@devcodes2206?source=rss-3fdc2a0b87e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*epmTOWD-VRn57p-y</url>
            <title>Stories by Devcodes on Medium</title>
            <link>https://medium.com/@devcodes2206?source=rss-3fdc2a0b87e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:48:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@devcodes2206/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Modern Node.js Backend: Build It Like a Pro]]></title>
            <link>https://medium.com/@devcodes2206/modern-node-js-backend-build-it-like-a-pro-6c5cdcab918f?source=rss-3fdc2a0b87e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c5cdcab918f</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Devcodes]]></dc:creator>
            <pubDate>Tue, 09 Dec 2025 10:54:26 GMT</pubDate>
            <atom:updated>2025-12-09T10:54:26.843Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z_QxDbDHULFjuJn1o3eF5Q.png" /></figure><p>Building a scalable and maintainable backend requires careful architectural decisions and clean code organization. In this comprehensive guide, I’ll walk you through creating a professional-grade backend using Node.js, Express, and MongoDB, with a focus on best practices and clean architecture.</p><h3>The Technology Stack</h3><p>We’ll be using:</p><ul><li><strong>Runtime</strong>: Node.js with ES modules</li><li><strong>Framework</strong>: Express.js</li><li><strong>Database</strong>: MongoDB via Mongoose</li><li><strong>Validation</strong>: Zod for runtime type checking</li><li><strong>Authentication</strong>: JWT-based access tokens</li><li><strong>Password Hashing</strong>: bcryptjs</li></ul><h3>High-Level Architecture Overview</h3><p>Our backend is organized into clear, maintainable layers:</p><h3>Entry &amp; App Setup</h3><ul><li>index.mjs – Starts the HTTP server</li><li>app.mjs – Configures Express app, middleware, and mounts route modules</li></ul><h3>Configuration &amp; Constants</h3><ul><li>constants/config.mjs – Loads environment variables and exposes a strongly-typed config object</li><li>constants/constants.mjs – Shared constants (HTTP status codes, messages, regex patterns)</li></ul><h3>Database Layer</h3><ul><li>db/database.mjs – Initializes MongoDB connection using Mongoose</li></ul><h3>Domain Modules</h3><ul><li><strong>User/Auth</strong> — User registration, login, JWT authentication</li><li><strong>Courses &amp; Purchases</strong> — Course listings and purchase tracking</li><li><strong>Forms</strong> — Form submission handling</li><li><strong>Metrics</strong> — Application metrics</li><li><strong>Subscriptions</strong> — Newsletter/email subscriber management</li></ul><h3>Cross-Cutting Concerns</h3><ul><li>middlewares/auth.mjs – Authentication and authorization middleware</li><li>validators/*.mjs – Zod-based request validation</li><li>repository/*.mjs – Data access layer per domain</li><li>models/*.mjs – Mongoose schemas for all entities</li><li>utils/*.mjs – Helper utilities (ApiError, ApiResponse, etc.)</li></ul><p>All modules use <strong>ES modules</strong> (type: &quot;module&quot; in package.json).</p><h3>Project Structure</h3><p>Here’s the complete project layout:</p><pre>backend/<br>├── index.mjs                  # Server entry point<br>├── app.mjs                    # Express app configuration<br>├── package.json               # Dependencies and scripts<br>├── sample.env                 # Environment variables template<br>├── constants/<br>│   ├── config.mjs            # Configuration management<br>│   └── constants.mjs         # Shared constants<br>├── db/<br>│   └── database.mjs          # MongoDB connection<br>├── models/<br>│   ├── user.model.mjs        # User schema<br>│   ├── course.model.mjs      # Course schema<br>│   ├── slide.model.mjs       # Slide schema<br>│   ├── video.model.mjs       # Video schema<br>│   ├── purchase.model.mjs    # Purchase schema<br>│   ├── form.model.mjs        # Form schema<br>│   ├── metric.model.mjs      # Metric schema<br>│   └── subscription.model.mjs # Subscription schema<br>├── repository/<br>│   ├── user.repository.mjs<br>│   ├── course.repository.mjs<br>│   ├── forms.repository.mjs<br>│   ├── metric.repository.mjs<br>│   └── subscription.repository.mjs<br>├── validators/<br>│   ├── user.validator.mjs<br>│   ├── forms.validator.mjs<br>│   └── metric.validator.mjs<br>├── controllers/<br>│   ├── user.controller.mjs<br>│   ├── course.controller.mjs<br>│   ├── forms.controller.mjs<br>│   ├── metric.controller.mjs<br>│   └── subscription.controller.mjs<br>├── routes/<br>│   ├── user.routes.mjs<br>│   ├── course.routes.mjs<br>│   ├── forms.routes.mjs<br>│   ├── metric.routes.mjs<br>│   └── subscription.routes.mjs<br>├── middlewares/<br>│   └── auth.mjs              # Authentication middleware<br>└── utils/<br>    ├── ApiError.mjs          # Custom error class<br>    ├── ApiResponse.mjs       # Standardized responses<br>    └── trimStrings.mjs       # String trimming utility</pre><h3>Setting Up the Foundation</h3><h3>1. App Configuration (app.mjs)</h3><p>The main Express app setup:</p><pre>import express from &#39;express&#39;;<br>import { connectDB } from &#39;./db/database.mjs&#39;;<br>import userRoutes from &#39;./routes/user.routes.mjs&#39;;<br>import courseRoutes from &#39;./routes/course.routes.mjs&#39;;<br>import formsRoutes from &#39;./routes/forms.routes.mjs&#39;;<br>import metricRoutes from &#39;./routes/metric.routes.mjs&#39;;<br>import subscriptionRoutes from &#39;./routes/subscription.routes.mjs&#39;;</pre><pre>const app = express();</pre><pre>// Connect to MongoDB<br>await connectDB();</pre><pre>// Core middleware<br>app.use(express.json());<br>app.use(express.urlencoded({ extended: true }));</pre><pre>// Health check endpoint<br>app.get(&#39;/&#39;, (req, res) =&gt; {<br>  res.json({ message: &#39;Server is Live&#39; });<br>});</pre><pre>// Mount API routes<br>app.use(&#39;/api&#39;, userRoutes);<br>app.use(&#39;/api&#39;, courseRoutes);<br>app.use(&#39;/api&#39;, formsRoutes);<br>app.use(&#39;/api&#39;, metricRoutes);<br>app.use(&#39;/api&#39;, subscriptionRoutes);</pre><pre>export default app;</pre><h3>2. Server Entry Point (index.mjs)</h3><pre>import app from &#39;./app.mjs&#39;;<br>import { config } from &#39;./constants/config.mjs&#39;;</pre><pre>app.listen(config.PORT, () =&gt; {<br>  console.log(`Server running on <a href="http://localhost:${config.PORT}`);">http://localhost:${config.PORT}`);</a><br>});</pre><h3>3. Package Configuration (package.json)</h3><pre>{<br>  &quot;name&quot;: &quot;backend&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;main&quot;: &quot;index.mjs&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;nodemon index.mjs&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.18.2&quot;,<br>    &quot;mongoose&quot;: &quot;^7.0.0&quot;,<br>    &quot;jsonwebtoken&quot;: &quot;^9.0.0&quot;,<br>    &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,<br>    &quot;dotenv&quot;: &quot;^16.0.3&quot;,<br>    &quot;cors&quot;: &quot;^2.8.5&quot;,<br>    &quot;zod&quot;: &quot;^3.21.4&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;nodemon&quot;: &quot;^2.0.22&quot;<br>  }<br>}</pre><h3>Environment &amp; Configuration Management</h3><h3>Configuration File (constants/config.mjs)</h3><pre>import dotenv from &#39;dotenv&#39;;<br>dotenv.config();</pre><pre>export const config = {<br>  // Server &amp; API<br>  PORT: process.env.PORT || 5000,<br>  API_URL: process.env.API_URL || &#39;http://localhost:5000/api&#39;,<br>  <br>  // Database<br>  DB_HOST: process.env.DB_HOST,<br>  DB_NAME: process.env.DB_NAME,<br>  DB_PASSWORD: process.env.DB_PASSWORD,<br>  MONGODB_URI: process.env.MONGODB_URI || &#39;mongodb://localhost:27017/myapp&#39;,<br>  <br>  // Caching<br>  REDIS_URL: process.env.REDIS_URL,<br>  <br>  // Authentication<br>  JWT_SECRET: process.env.JWT_SECRET,<br>  <br>  // Cloud Storage (Cloudinary)<br>  CLOUDINARY_CLOUD_NAME: process.env.CLOUDINARY_CLOUD_NAME,<br>  CLOUDINARY_API_KEY: process.env.CLOUDINARY_API_KEY,<br>  CLOUDINARY_API_SECRET: process.env.CLOUDINARY_API_SECRET,<br>  CLOUDINARY_URL: process.env.CLOUDINARY_URL,<br>};</pre><h3>Environment Variables Template (sample.env)</h3><pre>PORT=5000<br>DB_HOST=localhost<br>DB_NAME=myapp<br>JWT_SECRET=your_super_secret_jwt_key_here<br>MONGODB_URI=mongodb://localhost:27017/myapp<br>REDIS_URL=redis://localhost:6379<br>CLOUDINARY_CLOUD_NAME=your_cloud_name<br>CLOUDINARY_API_KEY=your_api_key<br>CLOUDINARY_API_SECRET=your_api_secret<br>CLOUDINARY_URL=cloudinary://...</pre><blockquote><strong><em>Security Note:</em></strong><em> Always keep secrets (especially </em><em>JWT_SECRET and </em><em>MONGODB_URI) out of source control and configure them per environment.</em></blockquote><h3>Centralized Constants (constants/constants.mjs)</h3><pre>export const HTTP_STATUS = {<br>  OK: 200,<br>  CREATED: 201,<br>  BAD_REQUEST: 400,<br>  UNAUTHORIZED: 401,<br>  FORBIDDEN: 403,<br>  NOT_FOUND: 404,<br>  INTERNAL_SERVER_ERROR: 500,<br>};</pre><pre>export const MESSAGES = {<br>  SUCCESS: &#39;Success&#39;,<br>  USER_NOT_FOUND: &#39;User not found&#39;,<br>  USER_ALREADY_EXISTS: &#39;User already exists&#39;,<br>  INVALID_INPUT: &#39;Invalid input&#39;,<br>  SERVER_ERROR: &#39;Internal server error&#39;,<br>};</pre><pre>export const AUTH = {<br>  ACCESS_TOKEN_EXPIRY: &#39;4h&#39;,<br>  REFRESH_TOKEN_EXPIRY: &#39;7d&#39;,<br>};</pre><pre>export const PAGINATION = {<br>  DEFAULT_PAGE: 1,<br>  DEFAULT_LIMIT: 10,<br>  MAX_LIMIT: 100,<br>};</pre><pre>export const UPLOADS = {<br>  MAX_FILE_SIZE: 5 * 1024 * 1024, // 5MB<br>  ALLOWED_FILE_TYPES: [&#39;image/jpeg&#39;, &#39;image/png&#39;, &#39;image/gif&#39;],<br>};</pre><pre>export const ENV = {<br>  DEV: &#39;development&#39;,<br>  PROD: &#39;production&#39;,<br>  TEST: &#39;test&#39;,<br>};</pre><pre>export const REGEX = {<br>  EMAIL: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,<br>  PHONE: /^[1-9]\d{9}$/,<br>  PASSWORD: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&amp;])[A-Za-z\d@$!%*?&amp;]{8,}$/,<br>  MONGO_ID: /^[a-f\d]{24}$/i,<br>  MONGO_ID_2: /^[0-9a-fA-F]{24}$/,<br>  SEBI_REGISTRATION_NUMBER: /^[A-Z]{3}\d{9}$/,<br>};</pre><p>These constants are reused throughout the application for consistency.</p><h3>Database Connection (db/database.mjs)</h3><pre>import mongoose from &#39;mongoose&#39;;<br>import { config } from &#39;../constants/config.mjs&#39;;</pre><pre>export const connectDB = async () =&gt; {<br>  try {<br>    await mongoose.connect(config.MONGODB_URI);<br>    console.log(&#39;MongoDB connected successfully&#39;);<br>  } catch (error) {<br>    console.error(&#39;MongoDB connection error:&#39;, error);<br>    process.exit(1);<br>  }<br>};</pre><p><strong>Pattern:</strong> Connect once at startup; never reconnect per request.</p><h3>Data Models (Mongoose Schemas)</h3><h3>1. User Model (models/user.model.mjs)</h3><pre>import mongoose from &#39;mongoose&#39;;<br>import { REGEX } from &#39;../constants/constants.mjs&#39;;</pre><pre>const userSchema = new mongoose.Schema(<br>  {<br>    first_name: {<br>      type: String,<br>      required: true,<br>      trim: true,<br>      minlength: 2,<br>      match: /^[a-zA-Z]+$/,<br>    },<br>    last_name: {<br>      type: String,<br>      required: true,<br>      trim: true,<br>      minlength: 2,<br>      match: /^[a-zA-Z]+$/,<br>    },<br>    email: {<br>      type: String,<br>      required: true,<br>      unique: true,<br>      trim: true,<br>      lowercase: true,<br>      match: REGEX.EMAIL,<br>    },<br>    phone_number: {<br>      type: String,<br>      required: true,<br>      unique: true,<br>      match: REGEX.PHONE,<br>    },<br>    password: {<br>      type: String,<br>      required: true,<br>    },<br>  },<br>  { timestamps: true }<br>);</pre><pre>export default mongoose.model(&#39;User&#39;, userSchema);</pre><h3>2. Course Model (models/course.model.mjs)</h3><pre>import mongoose from &#39;mongoose&#39;;</pre><pre>const courseSchema = new mongoose.Schema(<br>  {<br>    courseId: {<br>      type: String,<br>      required: true,<br>      unique: true,<br>      trim: true,<br>      lowercase: true,<br>    },<br>    title: {<br>      type: String,<br>      required: true,<br>      trim: true,<br>    },<br>    description: {<br>      type: String,<br>      required: true,<br>      trim: true,<br>    },<br>    price: {<br>      type: Number,<br>      required: true,<br>      min: 0,<br>    },<br>    thumbnail: {<br>      type: String,<br>      required: true,<br>    },<br>    isPublished: {<br>      type: Boolean,<br>      default: false,<br>    },<br>  },<br>  { timestamps: true }<br>);</pre><pre>// Virtual relationships<br>courseSchema.virtual(&#39;slides&#39;, {<br>  ref: &#39;Slide&#39;,<br>  localField: &#39;_id&#39;,<br>  foreignField: &#39;course&#39;,<br>});</pre><pre>courseSchema.virtual(&#39;videos&#39;, {<br>  ref: &#39;Video&#39;,<br>  localField: &#39;_id&#39;,<br>  foreignField: &#39;course&#39;,<br>});</pre><pre>// Enable virtuals in JSON/Object output<br>courseSchema.set(&#39;toObject&#39;, { virtuals: true });<br>courseSchema.set(&#39;toJSON&#39;, { virtuals: true });</pre><pre>export default mongoose.model(&#39;Course&#39;, courseSchema);</pre><h3>3. Purchase Model (models/purchase.model.mjs)</h3><pre>import mongoose from &#39;mongoose&#39;;</pre><pre>const purchaseSchema = new mongoose.Schema(<br>  {<br>    userId: {<br>      type: mongoose.Schema.Types.ObjectId,<br>      ref: &#39;User&#39;,<br>      required: true,<br>    },<br>    courseId: {<br>      type: mongoose.Schema.Types.ObjectId,<br>      ref: &#39;Course&#39;,<br>      required: true,<br>    },<br>    pricePaid: {<br>      type: Number,<br>      required: true,<br>      min: 0,<br>    },<br>    purchasedAt: {<br>      type: Date,<br>      default: Date.now,<br>    },<br>    paymentMethod: {<br>      type: String,<br>      enum: [&#39;card&#39;, &#39;upi&#39;, &#39;netbanking&#39;, &#39;wallet&#39;],<br>    },<br>    transactionId: {<br>      type: String,<br>      unique: true,<br>    },<br>  },<br>  { timestamps: true }<br>);</pre><pre>// Compound index: user can&#39;t purchase same course twice<br>purchaseSchema.index({ userId: 1, courseId: 1 }, { unique: true });</pre><pre>export default mongoose.model(&#39;Purchase&#39;, purchaseSchema);</pre><h3>4. Additional Models</h3><p>Similarly, create models for:</p><ul><li><strong>Slide Model</strong> — Course slide content</li><li><strong>Video Model</strong> — Course video content</li><li><strong>Form Model</strong> — Form submissions</li><li><strong>Metric Model</strong> — Application metrics</li><li><strong>Subscription Model</strong> — Email subscribers</li></ul><h3>Repository Layer (Data Access)</h3><p>The repository pattern encapsulates all database operations, keeping controllers clean.</p><h3>User Repository (repository/user.repository.mjs)</h3><pre>import User from &#39;../models/user.model.mjs&#39;;</pre><pre>export const findUserByEmail = async (email) =&gt; {<br>  return await User.findOne({ email });<br>};</pre><pre>export const createUser = async (userData) =&gt; {<br>  const user = new User(userData);<br>  return await user.save();<br>};</pre><pre>export const findUserById = async (userId) =&gt; {<br>  return await User.findById(userId).select(&#39;-password&#39;);<br>};</pre><pre>export const updateUserById = async (userId, updateData) =&gt; {<br>  return await User.findByIdAndUpdate(userId, updateData, { new: true });<br>};</pre><h3>Course Repository (repository/course.repository.mjs)</h3><pre>import Course from &#39;../models/course.model.mjs&#39;;<br>import Purchase from &#39;../models/purchase.model.mjs&#39;;</pre><pre>export const findPublishedCourses = async () =&gt; {<br>  return await Course.find({ isPublished: true });<br>};</pre><pre>export const findCourseById = async (courseId) =&gt; {<br>  return await Course.findById(courseId)<br>    .populate(&#39;slides&#39;)<br>    .populate(&#39;videos&#39;);<br>};</pre><pre>export const purchasedCourses = async (userId) =&gt; {<br>  const purchases = await Purchase.find({ userId })<br>    .populate({<br>      path: &#39;courseId&#39;,<br>      populate: [<br>        { path: &#39;slides&#39; },<br>        { path: &#39;videos&#39; }<br>      ]<br>    });<br>  <br>  return purchases.map(p =&gt; p.courseId);<br>};</pre><pre>export const createPurchase = async (purchaseData) =&gt; {<br>  const purchase = new Purchase(purchaseData);<br>  return await purchase.save();<br>};</pre><h3>Input Validation with Zod</h3><h3>User Validator (validators/user.validator.mjs)</h3><pre>import { z } from &#39;zod&#39;;<br>import { REGEX } from &#39;../constants/constants.mjs&#39;;<br>import { trimStrings } from &#39;../utils/trimStrings.mjs&#39;;</pre><pre>const signupSchema = z.object({<br>  first_name: z.string().min(2).regex(/^[a-zA-Z]+$/),<br>  last_name: z.string().min(2).regex(/^[a-zA-Z]+$/),<br>  email: z.string().email().regex(REGEX.EMAIL),<br>  phone_number: z.string().regex(REGEX.PHONE),<br>  password: z.string().regex(REGEX.PASSWORD),<br>});</pre><pre>export const validateSignup = (data) =&gt; {<br>  const trimmed = trimStrings(data);<br>  const result = signupSchema.safeParse(trimmed);<br>  <br>  if (!result.success) {<br>    return {<br>      success: false,<br>      errors: result.error.errors.map(e =&gt; e.message),<br>      message: &#39;Validation failed&#39;,<br>    };<br>  }<br>  <br>  return {<br>    success: true,<br>    data: result.data,<br>  };<br>};</pre><pre>const loginSchema = z.object({<br>  email: z.string().email(),<br>  password: z.string().min(1),<br>});</pre><pre>export const validateLogin = (data) =&gt; {<br>  const trimmed = trimStrings(data);<br>  const result = loginSchema.safeParse(trimmed);<br>  <br>  if (!result.success) {<br>    return {<br>      success: false,<br>      errors: result.error.errors.map(e =&gt; e.message),<br>      message: &#39;Validation failed&#39;,<br>    };<br>  }<br>  <br>  return {<br>    success: true,<br>    data: result.data,<br>  };<br>};</pre><h3>Authentication Middleware (middlewares/auth.mjs)</h3><pre>import jwt from &#39;jsonwebtoken&#39;;<br>import { config } from &#39;../constants/config.mjs&#39;;<br>import { HTTP_STATUS, MESSAGES } from &#39;../constants/constants.mjs&#39;;<br>import { findUserById } from &#39;../repository/user.repository.mjs&#39;;</pre><pre>export const authenticate = async (req, res, next) =&gt; {<br>  try {<br>    const authHeader = req.headers.authorization;<br>    <br>    if (!authHeader || !authHeader.startsWith(&#39;Bearer &#39;)) {<br>      return res.status(HTTP_STATUS.UNAUTHORIZED).json({<br>        message: &#39;Access denied. No token provided.&#39;,<br>      });<br>    }<br>    <br>    const token = authHeader.substring(7); // Remove &#39;Bearer &#39; prefix<br>    <br>    try {<br>      const decoded = jwt.verify(token, config.JWT_SECRET);<br>      const user = await findUserById(decoded.id);<br>      <br>      if (!user) {<br>        return res.status(HTTP_STATUS.UNAUTHORIZED).json({<br>          message: MESSAGES.USER_NOT_FOUND,<br>        });<br>      }<br>      <br>      req.user = { id: user._id };<br>      next();<br>    } catch (jwtError) {<br>      return res.status(HTTP_STATUS.UNAUTHORIZED).json({<br>        message: &#39;Invalid token&#39;,<br>      });<br>    }<br>  } catch (error) {<br>    console.error(&#39;Authentication error:&#39;, error);<br>    return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><pre>export const authorize = (roles = []) =&gt; {<br>  return (req, res, next) =&gt; {<br>    if (!roles.includes(req.user.role)) {<br>      return res.status(HTTP_STATUS.FORBIDDEN).json({<br>        message: &#39;Access denied. Insufficient permissions.&#39;,<br>      });<br>    }<br>    next();<br>  };<br>};</pre><h3>Controllers (Request Handlers)</h3><h3>User Controller (controllers/user.controller.mjs)</h3><pre>import bcrypt from &#39;bcryptjs&#39;;<br>import jwt from &#39;jsonwebtoken&#39;;<br>import { validateSignup, validateLogin } from &#39;../validators/user.validator.mjs&#39;;<br>import { findUserByEmail, createUser } from &#39;../repository/user.repository.mjs&#39;;<br>import { HTTP_STATUS, MESSAGES, AUTH } from &#39;../constants/constants.mjs&#39;;<br>import { config } from &#39;../constants/config.mjs&#39;;</pre><pre>export const signup = async (req, res) =&gt; {<br>  try {<br>    const validation = validateSignup(req.body);<br>    <br>    if (!validation.success) {<br>      return res.status(HTTP_STATUS.BAD_REQUEST).json({<br>        errors: validation.errors,<br>        message: validation.message,<br>      });<br>    }<br>    <br>    const { first_name, last_name, email, phone_number, password } = validation.data;<br>    <br>    // Check if user already exists<br>    const existingUser = await findUserByEmail(email);<br>    if (existingUser) {<br>      return res.status(HTTP_STATUS.BAD_REQUEST).json({<br>        message: MESSAGES.USER_ALREADY_EXISTS,<br>      });<br>    }<br>    <br>    // Hash password<br>    const hashedPassword = await bcrypt.hash(password, 10);<br>    <br>    // Create user<br>    const user = await createUser({<br>      first_name,<br>      last_name,<br>      email,<br>      phone_number,<br>      password: hashedPassword,<br>    });<br>    <br>    // Generate JWT<br>    const token = jwt.sign(<br>      { id: user._id },<br>      config.JWT_SECRET,<br>      { expiresIn: AUTH.ACCESS_TOKEN_EXPIRY }<br>    );<br>    <br>    res.status(HTTP_STATUS.CREATED).json({<br>      message: &#39;User created successfully&#39;,<br>      data: {<br>        user: {<br>          id: user._id,<br>          first_name: user.first_name,<br>          last_name: user.last_name,<br>          email: user.email,<br>        },<br>        token,<br>      },<br>    });<br>  } catch (error) {<br>    console.error(&#39;Signup error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><pre>export const login = async (req, res) =&gt; {<br>  try {<br>    const validation = validateLogin(req.body);<br>    <br>    if (!validation.success) {<br>      return res.status(HTTP_STATUS.BAD_REQUEST).json({<br>        errors: validation.errors,<br>        message: validation.message,<br>      });<br>    }<br>    <br>    const { email, password } = validation.data;<br>    <br>    // Find user<br>    const user = await findUserByEmail(email);<br>    if (!user) {<br>      return res.status(HTTP_STATUS.UNAUTHORIZED).json({<br>        message: &#39;Invalid credentials&#39;,<br>      });<br>    }<br>    <br>    // Verify password<br>    const isPasswordValid = await bcrypt.compare(password, user.password);<br>    if (!isPasswordValid) {<br>      return res.status(HTTP_STATUS.UNAUTHORIZED).json({<br>        message: &#39;Invalid credentials&#39;,<br>      });<br>    }<br>    <br>    // Generate JWT<br>    const token = jwt.sign(<br>      { id: user._id },<br>      config.JWT_SECRET,<br>      { expiresIn: AUTH.ACCESS_TOKEN_EXPIRY }<br>    );<br>    <br>    res.status(HTTP_STATUS.OK).json({<br>      message: &#39;Login successful&#39;,<br>      data: {<br>        user: {<br>          id: user._id,<br>          first_name: user.first_name,<br>          last_name: user.last_name,<br>          email: user.email,<br>        },<br>        token,<br>      },<br>    });<br>  } catch (error) {<br>    console.error(&#39;Login error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><h3>Course Controller (controllers/course.controller.mjs)</h3><pre>import { <br>  findPublishedCourses, <br>  findCourseById, <br>  purchasedCourses,<br>  createPurchase <br>} from &#39;../repository/course.repository.mjs&#39;;<br>import { HTTP_STATUS, MESSAGES } from &#39;../constants/constants.mjs&#39;;</pre><pre>export const getAvailableCourses = async (req, res) =&gt; {<br>  try {<br>    const courses = await findPublishedCourses();<br>    <br>    res.status(HTTP_STATUS.OK).json({<br>      message: MESSAGES.SUCCESS,<br>      data: courses,<br>    });<br>  } catch (error) {<br>    console.error(&#39;Get courses error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><pre>export const getPurchasedCourses = async (req, res) =&gt; {<br>  try {<br>    const courses = await purchasedCourses(req.user.id);<br>    <br>    res.status(HTTP_STATUS.OK).json({<br>      message: MESSAGES.SUCCESS,<br>      data: courses,<br>    });<br>  } catch (error) {<br>    console.error(&#39;Get purchased courses error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><pre>export const getCourseById = async (req, res) =&gt; {<br>  try {<br>    const { courseId } = req.params;<br>    const course = await findCourseById(courseId);<br>    <br>    if (!course) {<br>      return res.status(HTTP_STATUS.NOT_FOUND).json({<br>        message: &#39;Course not found&#39;,<br>      });<br>    }<br>    <br>    res.status(HTTP_STATUS.OK).json({<br>      message: MESSAGES.SUCCESS,<br>      data: course,<br>    });<br>  } catch (error) {<br>    console.error(&#39;Get course error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><pre>export const purchaseCourse = async (req, res) =&gt; {<br>  try {<br>    const { courseId } = req.params;<br>    const { transactionId, paymentMethod, pricePaid } = req.body;<br>    <br>    const course = await findCourseById(courseId);<br>    if (!course) {<br>      return res.status(HTTP_STATUS.NOT_FOUND).json({<br>        message: &#39;Course not found&#39;,<br>      });<br>    }<br>    <br>    const purchase = await createPurchase({<br>      userId: req.user.id,<br>      courseId,<br>      pricePaid,<br>      paymentMethod,<br>      transactionId,<br>    });<br>    <br>    res.status(HTTP_STATUS.CREATED).json({<br>      message: &#39;Course purchased successfully&#39;,<br>      data: purchase,<br>    });<br>  } catch (error) {<br>    console.error(&#39;Purchase course error:&#39;, error);<br>    res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({<br>      message: MESSAGES.SERVER_ERROR,<br>    });<br>  }<br>};</pre><h3>Routes (API Endpoints)</h3><h3>User Routes (routes/user.routes.mjs)</h3><pre>import express from &#39;express&#39;;<br>import { signup, login } from &#39;../controllers/user.controller.mjs&#39;;</pre><pre>const router = express.Router();</pre><pre>router.post(&#39;/auth/signup&#39;, signup);<br>router.post(&#39;/auth/login&#39;, login);</pre><pre>export default router;</pre><h3>Course Routes (routes/course.routes.mjs)</h3><pre>import express from &#39;express&#39;;<br>import { authenticate } from &#39;../middlewares/auth.mjs&#39;;<br>import {<br>  getAvailableCourses,<br>  getPurchasedCourses,<br>  getCourseById,<br>  purchaseCourse,<br>} from &#39;../controllers/course.controller.mjs&#39;;</pre><pre>const router = express.Router();</pre><pre>router.get(&#39;/courses/available&#39;, getAvailableCourses);<br>router.get(&#39;/courses/purchased&#39;, authenticate, getPurchasedCourses);<br>router.get(&#39;/courses/:courseId&#39;, authenticate, getCourseById);<br>router.post(&#39;/courses/:courseId/purchase&#39;, authenticate, purchaseCourse);</pre><pre>export default router;</pre><h3>Request Lifecycle Example</h3><p>Let’s trace an authenticated request:</p><p><strong>Request:</strong> GET /api/courses/purchased with Authorization: Bearer &lt;JWT&gt;</p><ol><li><strong>Express App</strong> receives the request</li><li><strong>JSON Parser</strong> middleware parses the body</li><li><strong>Router</strong> matches /api/courses/purchased</li><li><strong>Authenticate Middleware:</strong></li></ol><ul><li>Extracts JWT from Authorization header</li><li>Verifies token with JWT_SECRET</li><li>Loads user from database</li><li>Attaches req.user.id</li></ul><ol><li><strong>getPurchasedCourses Controller:</strong></li></ol><ul><li>Calls purchasedCourses(req.user.id) from repository</li><li>Repository performs Mongoose queries with population</li></ul><ol><li><strong>Response:</strong> 200 OK JSON with purchased courses array</li></ol><h3>Utility Classes</h3><h3>ApiError (utils/ApiError.mjs)</h3><pre>export class ApiError extends Error {<br>  constructor(statusCode, message, errors = []) {<br>    super(message);<br>    this.statusCode = statusCode;<br>    this.errors = errors;<br>    this.success = false;<br>  }<br>}</pre><h3>ApiResponse (utils/ApiResponse.mjs)</h3><pre>export class ApiResponse {<br>  constructor(statusCode, data, message = &#39;Success&#39;) {<br>    this.statusCode = statusCode;<br>    this.data = data;<br>    this.message = message;<br>    this.success = statusCode &lt; 400;<br>  }<br>}</pre><h3>String Trimming Utility (utils/trimStrings.mjs)</h3><pre>export const trimStrings = (obj) =&gt; {<br>  if (typeof obj !== &#39;object&#39; || obj === null) {<br>    return obj;<br>  }<br>  <br>  const trimmed = {};<br>  for (const [key, value] of Object.entries(obj)) {<br>    trimmed[key] = typeof value === &#39;string&#39; ? value.trim() : value;<br>  }<br>  <br>  return trimmed;<br>};</pre><h3>Best Practices &amp; Coding Style</h3><h3>1. ESM Only</h3><p>Always use import/export, never require().</p><h3>2. Async/Await</h3><p>Use async/await for all asynchronous operations with proper try/catch blocks.</p><h3>3. Validation First</h3><p>Always validate and normalize (trim) request bodies before database operations.</p><h3>4. Separation of Concerns</h3><ul><li><strong>Controllers</strong>: Orchestrate validation + repository calls + HTTP responses</li><li><strong>Repositories</strong>: Encapsulate all database access</li><li><strong>Models</strong>: Define schema, constraints, and indexes</li><li><strong>Middleware</strong>: Handle authentication and cross-cutting concerns</li></ul><h3>5. Consistent Response Format</h3><pre>// Success<br>{<br>  status: 200,<br>  message: &#39;Success&#39;,<br>  data: {...}<br>}</pre><pre>// Error<br>{<br>  status: 400,<br>  message: &#39;Validation failed&#39;,<br>  errors: [&#39;Error 1&#39;, &#39;Error 2&#39;]<br>}</pre><h3>6. Security Best Practices</h3><ul><li>Hash all passwords with bcryptjs</li><li>Store JWT secret in environment variables</li><li>Validate all user inputs with Zod</li><li>Use regex patterns for strict validation</li><li>Never expose passwords in API responses</li></ul><h3>Adding a New Feature</h3><p>When adding a new resource (e.g., Orders), follow this pattern:</p><h3>Step 1: Create Model</h3><pre>// models/order.model.mjs<br>import mongoose from &#39;mongoose&#39;;</pre><pre>const orderSchema = new mongoose.Schema({<br>  userId: { type: mongoose.Schema.Types.ObjectId, ref: &#39;User&#39;, required: true },<br>  items: [{ type: mongoose.Schema.Types.ObjectId, ref: &#39;Course&#39; }],<br>  totalAmount: { type: Number, required: true },<br>  status: { type: String, enum: [&#39;pending&#39;, &#39;completed&#39;, &#39;cancelled&#39;] },<br>}, { timestamps: true });</pre><pre>export default mongoose.model(&#39;Order&#39;, orderSchema);</pre><h3>Step 2: Create Validator</h3><pre>// validators/order.validator.mjs<br>import { z } from &#39;zod&#39;;</pre><pre>const orderSchema = z.object({<br>  items: z.array(z.string()),<br>  totalAmount: z.number().positive(),<br>});</pre><pre>export const validateOrder = (data) =&gt; {<br>  // ... validation logic<br>};</pre><h3>Step 3: Create Repository</h3><pre>// repository/order.repository.mjs<br>import Order from &#39;../models/order.model.mjs&#39;;</pre><pre>export const createOrder = async (orderData) =&gt; {<br>  const order = new Order(orderData);<br>  return await order.save();<br>};</pre><pre>export const findOrdersByUserId = async (userId) =&gt; {<br>  return await Order.find({ userId }).populate(&#39;items&#39;);<br>};</pre><h3>Step 4: Create Controller</h3><pre>// controllers/order.controller.mjs<br>import { validateOrder } from &#39;../validators/order.validator.mjs&#39;;<br>import { createOrder } from &#39;../repository/order.repository.mjs&#39;;<br>import { HTTP_STATUS } from &#39;../constants/constants.mjs&#39;;</pre><pre>export const placeOrder = async (req, res) =&gt; {<br>  // Validate, create order, return response<br>};</pre><h3>Step 5: Create Routes</h3><pre>// routes/order.routes.mjs<br>import express from &#39;express&#39;;<br>import { authenticate } from &#39;../middlewares/auth.mjs&#39;;<br>import { placeOrder, getOrders } from &#39;../controllers/order.controller.mjs&#39;;</pre><pre>const router = express.Router();</pre><pre>router.post(&#39;/orders&#39;, authenticate, placeOrder);<br>router.get(&#39;/orders&#39;, authenticate, getOrders);</pre><pre>export default router;</pre><h3>Step 6: Wire in App</h3><pre>// app.mjs<br>import orderRoutes from &#39;./routes/order.routes.mjs&#39;;<br>app.use(&#39;/api&#39;, orderRoutes);</pre><h3>Running the Application</h3><h3>Installation</h3><pre>cd backend<br>npm install</pre><h3>Environment Setup</h3><pre>cp sample.env .env<br># Edit .env with your configuration</pre><h3>Development</h3><pre>npm run dev</pre><p>The server will start on the configured port (default: 5000) with auto-reload via nodemon.</p><h3>Production</h3><pre>NODE_ENV=production node index.mjs</pre><p>Or use a process manager like PM2:</p><pre>pm2 start index.mjs --name &quot;backend-api&quot;</pre><h3>Troubleshooting</h3><h3>Cannot Connect to MongoDB</h3><ul><li>Verify MONGODB_URI in .env</li><li>Ensure MongoDB is running and accessible</li><li>Check firewall rules and network connectivity</li></ul><h3>JWT Errors</h3><ul><li>Verify client sends Authorization: Bearer &lt;token&gt; header</li><li>Ensure JWT_SECRET is consistent across all environments</li><li>Check token expiry settings</li></ul><h3>Validation Errors</h3><ul><li>Zod returns detailed error arrays</li><li>Inspect request body format</li><li>Ensure all required fields are present</li></ul><h3>Duplicate Key Errors</h3><ul><li>Check unique constraints on User (email, phone)</li><li>Verify transaction IDs are unique</li><li>Handle errors gracefully in controllers</li></ul><h3>Summary</h3><p>This architecture provides a <strong>clean, maintainable, and scalable</strong> foundation for your Node.js backend:</p><p>✅ <strong>Layered Architecture</strong> — Clear separation of concerns<br> ✅ <strong>Type-Safe Validation</strong> — Zod for runtime type checking<br> ✅ <strong>Secure Authentication</strong> — JWT-based with bcrypt password hashing<br> ✅ <strong>Consistent Code Style</strong> — ES modules throughout<br> ✅ <strong>Easy to Extend</strong> — Follow established patterns for new features</p><p>New contributors should follow the <strong>controller → repository → model → validator</strong> pattern for consistency and maintainability.</p><h3>What’s Next?</h3><p>Consider adding:</p><ul><li><strong>Rate Limiting</strong> — Protect against abuse</li><li><strong>Caching</strong> — Redis for frequently accessed data</li><li><strong>API Documentation</strong> — Swagger/OpenAPI specs</li><li><strong>Testing</strong> — Jest/Mocha for unit and integration tests</li><li><strong>Logging</strong> — Winston or Pino for structured logging</li><li><strong>Monitoring</strong> — Error tracking with Sentry</li><li><strong>CI/CD</strong> — Automated testing and deployment</li></ul><h3>CONTEXT.md for your cursor’s reference</h3><pre># CONTEXT.md - Backend Development Guide<br><br>&gt; **Quick Reference:** Production-ready Node.js + Express + MongoDB backend following clean architecture principles<br><br>## Table of Contents<br>1. [Project Overview](#project-overview)<br>2. [Tech Stack](#tech-stack)<br>3. [Architecture Layers](#architecture-layers)<br>4. [Directory Structure](#directory-structure)<br>5. [Core Conventions](#core-conventions)<br>6. [Configuration Management](#configuration-management)<br>7. [Database Layer](#database-layer)<br>8. [Models (Mongoose)](#models-mongoose)<br>9. [Repository Pattern](#repository-pattern)<br>10. [Validation Layer](#validation-layer)<br>11. [Controllers](#controllers)<br>12. [Middleware](#middleware)<br>13. [Routing](#routing)<br>14. [Authentication &amp; Authorization](#authentication--authorization)<br>15. [Error Handling](#error-handling)<br>16. [Response Formats](#response-formats)<br>17. [Adding New Features](#adding-new-features)<br>18. [Security Guidelines](#security-guidelines)<br>19. [Dos and Don&#39;ts](#dos-and-donts)<br>20. [Common Issues](#common-issues)<br><br>---<br><br>## Project Overview<br><br>**Type:** Production-ready REST API backend  <br>**Architecture:** Layered architecture with clean separation of concerns  <br>**Pattern:** Controllers → Repositories → Models  <br>**Module System:** ES Modules only (`.mjs` files)<br><br>### Key Characteristics<br>- JWT-based stateless authentication<br>- Zod runtime validation<br>- Repository pattern for data access<br>- Centralized configuration and constants<br>- Mongoose ODM for MongoDB<br><br>---<br><br>## Tech Stack<br><br>| Category | Technology | Version |<br>|----------|-----------|---------|<br>| Runtime | Node.js | Latest LTS |<br>| Framework | Express | ^4.18.2 |<br>| Database | MongoDB | ^5.0+ |<br>| ODM | Mongoose | ^7.0.0 |<br>| Validation | Zod | ^3.21.4 |<br>| Auth | JWT | ^9.0.0 |<br>| Password | bcryptjs | ^2.4.3 |<br>| Environment | dotenv | ^16.0.3 |<br><br>---<br><br>## Architecture Layers<br><br>```<br>HTTP Request<br>    ↓<br>Express Middleware (JSON parsing, CORS)<br>    ↓<br>Routes (URL mapping)<br>    ↓<br>Middleware (Authentication, Authorization)<br>    ↓<br>Controllers (Request orchestration)<br>    ↓<br>Validators (Zod validation)<br>    ↓<br>Repositories (Data access)<br>    ↓<br>Models (Mongoose schemas)<br>    ↓<br>MongoDB<br>```<br><br>### Layer Responsibilities<br><br>**Controllers:**<br>- Validate input via validators<br>- Orchestrate repository calls<br>- Return HTTP responses<br>- Handle errors gracefully<br><br>**Repositories:**<br>- Abstract all database operations<br>- Return raw data (no HTTP concerns)<br>- Use Mongoose models<br>- Keep business logic minimal<br><br>**Models:**<br>- Define Mongoose schemas<br>- Set validation rules<br>- Create indexes<br>- Define virtuals and methods<br><br>**Validators:**<br>- Use Zod for runtime validation<br>- Trim and normalize inputs<br>- Return consistent error format<br><br>**Middleware:**<br>- Authenticate JWT tokens<br>- Authorize based on roles<br>- Handle cross-cutting concerns<br><br>---<br><br>## Directory Structure<br><br>```<br>backend/<br>├── index.mjs                      # Server entry point<br>├── app.mjs                        # Express app setup<br>├── package.json                   # Dependencies (type: &quot;module&quot;)<br>├── .env                          # Environment variables (gitignored)<br>├── sample.env                    # Environment template<br>│<br>├── constants/<br>│   ├── config.mjs               # Environment config loader<br>│   └── constants.mjs            # HTTP status, messages, regex<br>│<br>├── db/<br>│   └── database.mjs             # MongoDB connection<br>│<br>├── models/<br>│   ├── user.model.mjs           # User schema<br>│   ├── course.model.mjs         # Course schema<br>│   ├── purchase.model.mjs       # Purchase schema<br>│   └── *.model.mjs              # Other models<br>│<br>├── repository/<br>│   ├── user.repository.mjs      # User data access<br>│   ├── course.repository.mjs    # Course data access<br>│   └── *.repository.mjs         # Other repositories<br>│<br>├── validators/<br>│   ├── user.validator.mjs       # User Zod schemas<br>│   └── *.validator.mjs          # Other validators<br>│<br>├── controllers/<br>│   ├── user.controller.mjs      # User request handlers<br>│   └── *.controller.mjs         # Other controllers<br>│<br>├── routes/<br>│   ├── user.routes.mjs          # User endpoints<br>│   └── *.routes.mjs             # Other routes<br>│<br>├── middlewares/<br>│   └── auth.mjs                 # Authentication &amp; authorization<br>│<br>└── utils/<br>    ├── ApiError.mjs             # Custom error class<br>    ├── ApiResponse.mjs          # Response wrapper<br>    └── trimStrings.mjs          # String sanitization<br>```<br><br>---<br><br>## Core Conventions<br><br>### File Naming<br>- **Extension:** Always `.mjs` (ES modules)<br>- **Format:** `{feature}.{layer}.mjs`<br>- **Examples:** `user.model.mjs`, `course.controller.mjs`, `auth.middleware.mjs`<br><br>### Module System<br>**✅ DO:** Use `import`/`export` (ES modules)  <br>**❌ DON&#39;T:** Use `require()`/`module.exports` (CommonJS)<br><br>### Naming Conventions<br>- **Variables/Functions:** camelCase (`userName`, `getUserById`)<br>- **Constants:** UPPER_SNAKE_CASE (`HTTP_STATUS`, `MAX_FILE_SIZE`)<br>- **Classes:** PascalCase (`ApiError`, `UserRepository`)<br>- **Files:** lowercase.with.dots (`user.model.mjs`)<br><br>### Import Order<br>1. Node.js built-ins<br>2. External packages<br>3. Internal absolute imports (constants, config)<br>4. Internal relative imports (models, repositories)<br><br>### Async Pattern<br>**✅ DO:** Use async/await for all async operations  <br>**❌ DON&#39;T:** Use promise chains (`.then()`, `.catch()`)  <br>**Always:** Wrap in try-catch blocks<br><br>---<br><br>## Configuration Management<br><br>### Environment Variables<br>- Loaded via `dotenv` in `constants/config.mjs`<br>- Never commit `.env` to version control<br>- Always provide `sample.env` template<br>- Use strong secrets (32+ characters for JWT_SECRET)<br><br>### Required Variables<br>- `PORT` - Server port (default: 5000)<br>- `MONGODB_URI` - Full MongoDB connection string<br>- `JWT_SECRET` - Token signing secret (strong, unique)<br>- `NODE_ENV` - Environment (development, production, test)<br><br>### Constants Structure<br>All shared constants live in `constants/constants.mjs`:<br>- `HTTP_STATUS` - Status codes (200, 201, 400, 401, 404, 500)<br>- `MESSAGES` - Standard messages (SUCCESS, USER_NOT_FOUND, etc.)<br>- `AUTH` - Auth settings (ACCESS_TOKEN_EXPIRY, SALT_ROUNDS)<br>- `PAGINATION` - Pagination defaults (DEFAULT_PAGE, DEFAULT_LIMIT)<br>- `REGEX` - Validation patterns (EMAIL, PHONE, PASSWORD, MONGO_ID)<br><br>**✅ DO:** Import constants instead of hardcoding  <br>**❌ DON&#39;T:** Use magic numbers or strings<br><br>---<br><br>## Database Layer<br><br>### Connection Pattern<br>- Connect once at app startup in `app.mjs`<br>- Never reconnect per request<br>- Exit process on connection failure<br>- Log connection status<br><br>### Mongoose Configuration<br>- Use Mongoose ODM (never raw MongoDB queries)<br>- Enable timestamps: `{ timestamps: true }`<br>- Use virtuals for derived data<br>- Enable virtuals in JSON: `{ toJSON: { virtuals: true } }`<br><br>**✅ DO:** Connect once at startup  <br>**❌ DON&#39;T:** Create new connections per request<br><br>---<br><br>## Models (Mongoose)<br><br>### Schema Structure<br>- Define all fields with proper types and validation<br>- Use `required`, `unique`, `trim`, `lowercase` where appropriate<br>- Set default values where sensible<br>- Use enums for fixed value sets<br>- Add custom validation with regex from constants<br><br>### Essential Features<br>- **Timestamps:** Always use `{ timestamps: true }`<br>- **Indexes:** Add indexes on frequently queried fields<br>- **Virtuals:** Use for computed/derived properties<br>- **Refs:** Use `ObjectId` references for relationships<br>- **Validation:** Use regex patterns from constants<br><br>### Relationships<br>- **One-to-Many:** Use refs and virtual populate<br>- **Many-to-Many:** Use array of refs<br>- **Embedded:** Use subdocuments for tightly coupled data<br><br>### Security<br>- Never expose passwords in JSON output<br>- Use `toJSON` transform to remove sensitive fields<br>- Select fields explicitly in queries<br><br>**✅ DO:**<br>- Use timestamps on all models<br>- Index frequently queried fields<br>- Use virtuals for derived data<br>- Reference other models with ObjectId<br>- Provide validation error messages<br><br>**❌ DON&#39;T:**<br>- Store computed values<br>- Expose passwords in responses<br>- Skip validation<br>- Use arbitrary field types<br><br>---<br><br>## Repository Pattern<br><br>### Purpose<br>Abstract all database operations away from controllers. Repositories are the ONLY place where Mongoose models are directly queried.<br><br>### Repository Functions<br>Each repository exports named functions for:<br>- **Create:** Insert new documents<br>- **Read:** Find documents (by ID, by field, with filters)<br>- **Update:** Modify existing documents<br>- **Delete:** Remove documents<br>- **Complex queries:** Aggregations, joins, pagination<br><br>### Key Principles<br>- One repository per model<br>- Export named functions (not default export)<br>- Always use async/await<br>- Return data (don&#39;t return responses)<br>- Don&#39;t catch errors (let them bubble up)<br>- Use `.populate()` for relationships<br>- Use `.select()` to exclude sensitive fields<br><br>**✅ DO:**<br>- Keep all database queries in repositories<br>- Return plain data from repositories<br>- Use descriptive function names<br>- Populate related data when needed<br><br>**❌ DON&#39;T:**<br>- Put business logic in repositories<br>- Return HTTP responses from repositories<br>- Catch errors in repositories<br>- Access models directly from controllers<br><br>---<br><br>## Validation Layer<br><br>### Zod Pattern<br>All input validation uses Zod schemas defined in `validators/*.validator.mjs`<br><br>### Validation Functions<br>- Accept raw input data<br>- Trim all strings using `trimStrings()` utility<br>- Use `safeParse()` (not `parse()`)<br>- Return consistent format: `{ success, data/errors, message }`<br>- Reuse regex patterns from constants<br><br>### Validation Types<br>- **Signup/Registration:** All user fields<br>- **Login:** Email + password only<br>- **Update:** Optional fields only<br>- **Forms:** Custom form fields<br>- **Queries:** Query parameters and filters<br><br>**✅ DO:**<br>- Validate ALL inputs before processing<br>- Trim strings before validation<br>- Use regex from constants<br>- Provide helpful error messages<br>- Return arrays of specific errors<br><br>**❌ DON&#39;T:**<br>- Skip validation for any endpoint<br>- Use `parse()` (throws errors)<br>- Hardcode regex patterns<br>- Return generic error messages<br><br>---<br><br>## Controllers<br><br>### Responsibilities<br>- Validate input using validators<br>- Call repository functions<br>- Handle business logic<br>- Return HTTP responses<br>- Catch and handle errors<br><br>### Controller Flow<br>1. Extract data from `req.body`, `req.params`, `req.query`<br>2. Validate input (return 400 if invalid)<br>3. Perform business logic checks<br>4. Call repository functions<br>5. Return success response (200, 201)<br>6. Catch errors and return appropriate status<br><br>### Error Handling<br>- Wrap everything in try-catch<br>- Handle specific errors (duplicate key, validation, cast)<br>- Log errors with context<br>- Return generic error messages to clients<br>- Use HTTP_STATUS constants<br><br>**✅ DO:**<br>- Validate first, process later<br>- Use try-catch in every controller<br>- Return consistent response formats<br>- Use HTTP_STATUS constants<br>- Log errors with function name<br><br>**❌ DON&#39;T:**<br>- Skip input validation<br>- Expose internal error details<br>- Hardcode status codes<br>- Let errors crash the server<br>- Query database directly (use repositories)<br><br>---<br><br>## Middleware<br><br>### Authentication (`authenticate`)<br>- Extract JWT from Authorization header (`Bearer &lt;token&gt;`)<br>- Verify token with JWT_SECRET<br>- Load user from database<br>- Check if user is active<br>- Attach minimal user data to `req.user` (id, email, role)<br>- Return 401 if token invalid/expired<br><br>### Authorization (`authorize`)<br>- Check if `req.user` exists (must run after authenticate)<br>- Verify user role matches allowed roles<br>- Return 403 if insufficient permissions<br><br>### Optional Auth (`optionalAuth`)<br>- Try to authenticate but don&#39;t fail if no token<br>- Useful for endpoints that work differently for authenticated users<br><br>### Middleware Order<br>Routes must stack middleware correctly:<br>```<br>router.get(&#39;/path&#39;, authenticate, authorize([&#39;admin&#39;]), controller)<br>```<br><br>**✅ DO:**<br>- Run authenticate before authorize<br>- Check user.isActive<br>- Attach minimal data to req.user<br>- Handle token expiry separately<br>- Use middleware composition<br><br>**❌ DON&#39;T:**<br>- Skip authentication on protected routes<br>- Expose JWT verification errors<br>- Attach entire user object to req<br>- Allow inactive users<br><br>---<br><br>## Routing<br><br>### RESTful Conventions<br>- **GET:** Read resources<br>- **POST:** Create resources<br>- **PUT/PATCH:** Update resources<br>- **DELETE:** Delete resources<br><br>### URL Structure<br>- Use plural nouns: `/users`, `/courses`<br>- Use resource IDs in path: `/users/:id`<br>- Nest related resources: `/courses/:courseId/videos`<br>- Mount all routes under `/api` prefix<br><br>### Route Organization<br>- One route file per feature<br>- Export default router<br>- Apply middleware before controllers<br>- Group public and protected routes<br><br>**✅ DO:**<br>- Follow REST conventions<br>- Use plural resource names<br>- Apply authentication middleware<br>- Mount routes under /api prefix<br>- Use route parameters for IDs<br><br>**❌ DON&#39;T:**<br>- Mix HTTP methods inconsistently<br>- Use verbs in URLs (/getUser, /createOrder)<br>- Skip authentication on sensitive routes<br>- Hardcode routes in controllers<br><br>---<br><br>## Authentication &amp; Authorization<br><br>### Authentication Flow<br>**Registration:**<br>1. Validate input<br>2. Check if user exists<br>3. Hash password with bcrypt<br>4. Create user in database<br>5. Generate JWT token<br>6. Return user + token<br><br>**Login:**<br>1. Validate input<br>2. Find user by email<br>3. Compare password with bcrypt<br>4. Check if user is active<br>5. Generate JWT token<br>6. Return user + token<br><br>**Protected Request:**<br>1. Extract token from header<br>2. Verify token<br>3. Load user from database<br>4. Attach to req.user<br>5. Continue to controller<br><br>### JWT Token<br>- Include: user ID, email, role<br>- Expiry: 4 hours (access token)<br>- Secret: Strong, environment-specific<br>- Never include: password, sensitive data<br><br>### Password Security<br>- Always hash with bcrypt (salt rounds: 10)<br>- Never store plain text passwords<br>- Never return passwords in responses<br>- Compare hashes, never decrypt<br><br>**✅ DO:**<br>- Hash all passwords before saving<br>- Use strong JWT secrets<br>- Set appropriate token expiry<br>- Verify user is active<br>- Check permissions explicitly<br><br>**❌ DON&#39;T:**<br>- Store plain passwords<br>- Include sensitive data in JWT<br>- Use weak secrets<br>- Skip user.isActive check<br>- Trust client-provided roles<br><br>---<br><br>## Error Handling<br><br>### HTTP Status Codes<br>- **200 OK:** Successful request<br>- **201 Created:** Resource created<br>- **400 Bad Request:** Invalid input<br>- **401 Unauthorized:** Missing/invalid token<br>- **403 Forbidden:** Insufficient permissions<br>- **404 Not Found:** Resource doesn&#39;t exist<br>- **409 Conflict:** Duplicate resource<br>- **500 Internal Server Error:** Server error<br><br>### Error Response Format<br>```json<br>{<br>  &quot;message&quot;: &quot;Error description&quot;,<br>  &quot;errors&quot;: [&quot;Specific error 1&quot;, &quot;Specific error 2&quot;]<br>}<br>```<br><br>### Specific Error Handling<br>- **Duplicate Key (11000):** Return 409 with field name<br>- **Validation Error:** Return 400 with error array<br>- **Cast Error (Invalid ObjectId):** Return 400<br>- **JWT Errors:** Return 401 with generic message<br><br>**✅ DO:**<br>- Use try-catch in all controllers<br>- Log errors with context<br>- Return generic messages to clients<br>- Handle specific error types<br>- Use HTTP_STATUS constants<br><br>**❌ DON&#39;T:**<br>- Expose stack traces in production<br>- Return internal error details<br>- Let errors crash server<br>- Use magic status codes<br>- Skip error logging<br><br>---<br><br>## Response Formats<br><br>### Success Response<br>```json<br>{<br>  &quot;message&quot;: &quot;Success message&quot;,<br>  &quot;data&quot;: {}<br>}<br>```<br><br>### Error Response<br>```json<br>{<br>  &quot;message&quot;: &quot;Error message&quot;,<br>  &quot;errors&quot;: [&quot;Optional array of specific errors&quot;]<br>}<br>```<br><br>### Pagination Response<br>```json<br>{<br>  &quot;message&quot;: &quot;Success&quot;,<br>  &quot;data&quot;: [],<br>  &quot;pagination&quot;: {<br>    &quot;page&quot;: 1,<br>    &quot;limit&quot;: 10,<br>    &quot;total&quot;: 100,<br>    &quot;pages&quot;: 10<br>  }<br>}<br>```<br><br>### Auth Response<br>```json<br>{<br>  &quot;message&quot;: &quot;Login successful&quot;,<br>  &quot;data&quot;: {<br>    &quot;user&quot;: {},<br>    &quot;token&quot;: &quot;jwt_token_here&quot;<br>  }<br>}<br>```<br><br>**✅ DO:**<br>- Return consistent response structure<br>- Include meaningful messages<br>- Paginate large datasets<br>- Exclude sensitive data<br><br>**❌ DON&#39;T:**<br>- Return inconsistent formats<br>- Include passwords in responses<br>- Send entire database documents<br>- Mix success/error formats<br><br>---<br><br>## Adding New Features<br><br>### Step-by-Step Process<br><br>**1. Model (`models/feature.model.mjs`)**<br>- Define Mongoose schema<br>- Add validation rules<br>- Create indexes<br>- Set up virtuals/refs<br><br>**2. Validator (`validators/feature.validator.mjs`)**<br>- Create Zod schemas<br>- Export validation functions<br>- Use trimStrings utility<br>- Reuse regex constants<br><br>**3. Repository (`repository/feature.repository.mjs`)**<br>- Create CRUD functions<br>- Add complex queries<br>- Use populate for refs<br>- Export named functions<br><br>**4. Controller (`controllers/feature.controller.mjs`)**<br>- Validate inputs<br>- Call repositories<br>- Handle errors<br>- Return responses<br><br>**5. Routes (`routes/feature.routes.mjs`)**<br>- Define endpoints<br>- Apply middleware<br>- Map to controllers<br>- Export default router<br><br>**6. Wire in App (`app.mjs`)**<br>- Import route file<br>- Mount under /api<br><br>**7. Test**<br>- Test all endpoints<br>- Verify authentication<br>- Check error cases<br>- Validate responses<br><br>---<br><br>## Security Guidelines<br><br>### Critical Security Rules<br><br>**Passwords:**<br>- Always hash with bcrypt (salt rounds: 10)<br>- Never store plaintext<br>- Never return in responses<br>- Use strong password regex<br><br>**JWT:**<br>- Use strong secret (32+ characters)<br>- Set appropriate expiry (4h access, 7d refresh)<br>- Include minimal payload (id, email, role)<br>- Verify on every protected request<br><br>**Input Validation:**<br>- Validate ALL inputs with Zod<br>- Trim and sanitize strings<br>- Use regex from constants<br>- Validate MongoDB ObjectIds<br><br>**Database:**<br>- Use Mongoose (prevents NoSQL injection)<br>- Use parameterized queries<br>- Set unique constraints<br>- Never interpolate user input<br><br>**Error Messages:**<br>- Generic messages in production<br>- No stack traces to clients<br>- Log details server-side only<br>- Don&#39;t expose internal structure<br><br>**Authorization:**<br>- Check authentication first<br>- Verify ownership of resources<br>- Check user roles explicitly<br>- Verify user.isActive status<br><br>---<br><br>## Dos and Don&#39;ts<br><br>### Module System<br>**✅ DO:**<br>- Use ES modules (`import`/`export`)<br>- Use `.mjs` file extension<br>- Use async/await for async operations<br><br>**❌ DON&#39;T:**<br>- Use CommonJS (`require`/`module.exports`)<br>- Use `.js` extension<br>- Use promise chains (`.then()`)<br><br>### Configuration<br>**✅ DO:**<br>- Load config from environment variables<br>- Use constants from `constants.mjs`<br>- Keep secrets in `.env` (gitignored)<br>- Provide `sample.env` template<br><br>**❌ DON&#39;T:**<br>- Hardcode configuration values<br>- Commit secrets to repository<br>- Use magic numbers or strings<br>- Skip environment validation<br><br>### Database<br>**✅ DO:**<br>- Connect once at startup<br>- Use Mongoose for all queries<br>- Add indexes on queried fields<br>- Use timestamps on all models<br><br>**❌ DON&#39;T:**<br>- Reconnect per request<br>- Use raw MongoDB queries<br>- Skip indexes<br>- Store computed values<br><br>### Validation<br>**✅ DO:**<br>- Validate ALL inputs with Zod<br>- Trim strings before validation<br>- Return specific error arrays<br>- Use regex from constants<br><br>**❌ DON&#39;T:**<br>- Skip validation<br>- Use `parse()` (use `safeParse()`)<br>- Return generic validation errors<br>- Hardcode regex patterns<br><br>### Controllers<br>**✅ DO:**<br>- Use try-catch in every controller<br>- Validate input first<br>- Call repositories for data<br>- Return consistent responses<br>- Log errors with context<br><br>**❌ DON&#39;T:**<br>- Skip error handling<br>- Query models directly<br>- Return inconsistent formats<br>- Expose internal errors<br>- Hardcode status codes<br><br>### Authentication<br>**✅ DO:**<br>- Hash passwords with bcrypt<br>- Use strong JWT secrets<br>- Verify tokens on protected routes<br>- Check user.isActive<br>- Set appropriate token expiry<br><br>**❌ DON&#39;T:**<br>- Store plain passwords<br>- Use weak secrets<br>- Skip authentication checks<br>- Include sensitive data in JWT<br>- Trust client-provided roles<br><br>### Error Handling<br>**✅ DO:**<br>- Catch all errors in controllers<br>- Handle specific error types<br>- Log errors server-side<br>- Return generic messages<br>- Use HTTP_STATUS constants<br><br>**❌ DON&#39;T:**<br>- Let errors crash server<br>- Expose stack traces<br>- Return internal details<br>- Hardcode status codes<br>- Skip error logging<br><br>### Security<br>**✅ DO:**<br>- Validate and sanitize all inputs<br>- Use HTTPS in production<br>- Check resource ownership<br>- Verify user permissions<br>- Keep dependencies updated<br><br>**❌ DON&#39;T:**<br>- Trust user input<br>- Send tokens over HTTP<br>- Skip authorization checks<br>- Expose sensitive data<br>- Ignore security updates<br><br>### Code Style<br>**✅ DO:**<br>- Use descriptive variable names<br>- Follow naming conventions<br>- Comment complex logic<br>- Keep functions focused<br>- Maintain consistent formatting<br><br>**❌ DON&#39;T:**<br>- Use single-letter variables (except i, j in loops)<br>- Mix naming styles<br>- Write monolithic functions<br>- Skip documentation<br>- Ignore linting rules<br><br>### API Design<br>**✅ DO:**<br>- Follow REST conventions<br>- Use plural resource names<br>- Version your API (/api/v1)<br>- Paginate large responses<br>- Document endpoints<br><br>**❌ DON&#39;T:**<br>- Use verbs in URLs<br>- Return entire collections unfiltered<br>- Skip pagination<br>- Change response formats<br>- Leave endpoints undocumented<br><br>---<br><br>## Common Issues<br><br>### Cannot Connect to MongoDB<br>**Symptoms:** `MongoServerError: bad auth`  <br>**Solutions:**<br>- Verify MONGODB_URI in .env<br>- Check username/password<br>- Verify network access (IP whitelist)<br>- Ensure MongoDB is running<br><br>### JWT Invalid Token<br>**Symptoms:** `JsonWebTokenError: invalid token`  <br>**Solutions:**<br>- Verify JWT_SECRET is consistent<br>- Check Authorization header format<br>- Token might be expired<br>- Ensure correct token is sent<br><br>### Validation Errors<br>**Symptoms:** Validation failed with Zod errors  <br>**Solutions:**<br>- Check required fields are present<br>- Verify data types match schema<br>- Check regex patterns (email, phone)<br>- Ensure strings are trimmed<br><br>### Duplicate Key Error<br>**Symptoms:** `E11000 duplicate key error`  <br>**Solutions:**<br>- Check unique fields (email, phone, transactionId)<br>- Clear test data<br>- Handle in controller (return 409)<br><br>### Module Import Errors<br>**Symptoms:** `Cannot find module`  <br>**Solutions:**<br>- Verify file extension is .mjs<br>- Check import path is correct<br>- Ensure package.json has &quot;type&quot;: &quot;module&quot;<br>- Use relative paths for local files<br><br>### Password Not Hashing<br>**Symptoms:** Login fails even with correct password  <br>**Solutions:**<br>- Ensure password is hashed before saving<br>- Use bcrypt.hash() in controller<br>- Check salt rounds constant<br>- Never hash in model (do it in controller)<br><br>### Middleware Not Running<br>**Symptoms:** req.user is undefined in controller  <br>**Solutions:**<br>- Ensure authenticate middleware is in route<br>- Check middleware order (authenticate before controller)<br>- Verify token is being sent<br>- Check middleware doesn&#39;t have errors<br><br>---<br><br>## Quick Reference<br><br>### File Naming<br>- Models: `{feature}.model.mjs`<br>- Controllers: `{feature}.controller.mjs`<br>- Repositories: `{feature}.repository.mjs`<br>- Validators: `{feature}.validator.mjs`<br>- Routes: `{feature}.routes.mjs`<br>- Middleware: `{purpose}.mjs`<br><br>### Controller Template Flow<br>1. Validate input<br>2. Business logic checks<br>3. Call repository<br>4. Return response<br>5. Handle errors in catch block<br><br>### Route Template Pattern<br>```<br>router.method(&#39;/path&#39;, authenticate, authorize([roles]), controller)<br>```<br><br>### Constants to Always Use<br>- `HTTP_STATUS.*` for status codes<br>- `MESSAGES.*` for standard messages<br>- `REGEX.*` for validation patterns<br>- `AUTH.*` for auth settings<br>- `PAGINATION.*` for pagination defaults<br><br>### Common Mongoose Patterns<br>- Use `{ timestamps: true }` on all schemas<br>- Use `.select(&#39;-password&#39;)` to exclude password<br>- Use `.populate()` for relationships<br>- Use indexes on frequently queried fields<br>- Use virtuals for computed properties<br><br>### Repository Pattern Rules<br>- One repository per model<br>- Export named functions only<br>- Always use async/await<br>- Return data, not responses<br>- Don&#39;t catch errors (bubble up)<br><br>### Validation Rules<br>- Always trim strings first<br>- Use `safeParse()` not `parse()`<br>- Return consistent format<br>- Reuse regex from constants<br>- Provide helpful error messages<br><br>---<br><br>**Version:** 1.0.0  <br>**Last Updated:** December 2024<br><br>**Remember:** <br>- Validate everything<br>- Hash all passwords<br>- Use constants over magic values<br>- Follow the layered architecture<br>- Keep security first</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c5cdcab918f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python Logging: Colored and Boxed Loggers for Readable Outputs]]></title>
            <link>https://medium.com/@devcodes2206/python-logging-colored-and-boxed-loggers-for-readable-outputs-4e57102a3ec5?source=rss-3fdc2a0b87e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e57102a3ec5</guid>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Devcodes]]></dc:creator>
            <pubDate>Mon, 08 Sep 2025 06:52:05 GMT</pubDate>
            <atom:updated>2025-09-08T10:24:07.599Z</atom:updated>
            <content:encoded><![CDATA[<h3>Python Logging: Colored and Boxed Loggers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQBJ7B4GqEeeuO2up4t-HA.png" /></figure><p>Logging is one of the most underrated parts of backend development. We all know print() debugging is tempting, but structured, colorful, and formatted logs can save hours of debugging pain.</p><p>In this article, I’ll show you <strong>two approaches to beautiful logging in Python</strong>:</p><ol><li><strong>Pure Colored Logging</strong> – simple, clean, color-coded logs.</li><li><strong>Boxed Logging</strong> – logs wrapped inside a “box” for high visibility, with two variations</li></ol><h3>1️⃣ Pure Colored Logging</h3><p>When working with frameworks like <strong>FastAPI</strong> or <strong>Uvicorn</strong>, plain logs can easily get lost in the noise. Adding colors makes it easy to scan for errors, warnings, and info messages.</p><p>Here’s how you can configure a <strong>colored logger</strong> using <a href="https://pypi.org/project/colorlog/">colorlog</a>:</p><pre>import logging.config<br><br>LOGGING = {<br>    &quot;version&quot;: 1,<br>    &quot;disable_existing_loggers&quot;: False,  # keep existing loggers (e.g., uvicorn) active<br>    &quot;formatters&quot;: {<br>        &quot;color&quot;: {<br>            &quot;()&quot;: &quot;colorlog.ColoredFormatter&quot;,<br>            &quot;format&quot;: (<br>                &quot;%(log_color)s%(levelname)-8s%(reset)s &quot;<br>                &quot;%(asctime)s &quot;<br>                &quot;%(blue)s[%(name)s]%(reset)s &quot;<br>                &quot;%(cyan)s%(pathname)s:%(lineno)d%(reset)s - &quot;<br>                &quot;%(message_log_color)s%(message)s%(reset)s&quot;<br>            ),<br>            &quot;datefmt&quot;: &quot;%Y-%m-%d %H:%M:%S&quot;,<br>            &quot;log_colors&quot;: {<br>                &quot;DEBUG&quot;: &quot;cyan&quot;,<br>                &quot;INFO&quot;: &quot;green&quot;,<br>                &quot;WARNING&quot;: &quot;yellow&quot;,<br>                &quot;ERROR&quot;: &quot;red&quot;,<br>                &quot;CRITICAL&quot;: &quot;bold_red&quot;,<br>            },<br>            &quot;secondary_log_colors&quot;: {<br>                &quot;message&quot;: {<br>                    &quot;DEBUG&quot;: &quot;cyan&quot;,<br>                    &quot;INFO&quot;: &quot;green&quot;,<br>                    &quot;WARNING&quot;: &quot;yellow&quot;,<br>                    &quot;ERROR&quot;: &quot;red&quot;,<br>                    &quot;CRITICAL&quot;: &quot;bold_red&quot;,<br>                }<br>            },<br>        },<br>    },<br>    &quot;handlers&quot;: {<br>        &quot;console&quot;: {<br>            &quot;class&quot;: &quot;logging.StreamHandler&quot;,<br>            &quot;formatter&quot;: &quot;color&quot;,<br>            &quot;level&quot;: &quot;DEBUG&quot;,<br>            &quot;stream&quot;: &quot;ext://sys.stdout&quot;,<br>        },<br>    },<br>    &quot;loggers&quot;: {<br>        &quot;&quot;: {&quot;handlers&quot;: [&quot;console&quot;], &quot;level&quot;: &quot;DEBUG&quot;},<br>        &quot;uvicorn.error&quot;: {&quot;level&quot;: &quot;INFO&quot;, &quot;propagate&quot;: True},<br>        &quot;uvicorn.access&quot;: {&quot;handlers&quot;: [&quot;console&quot;], &quot;level&quot;: &quot;INFO&quot;, &quot;propagate&quot;: False},<br>    },<br>}<br><br>def setup_logging():<br>    logging.config.dictConfig(LOGGING)</pre><p>📌 Example output:</p><pre>INFO     2025-09-08 12:30:45 [main] /app/main.py:25 - Server started successfully<br>ERROR    2025-09-08 12:31:12 [db] /app/db.py:45 - Database connection failed</pre><p>Each level has its own color, making it super easy to skim logs.</p><h3>2️⃣ Boxed Logging</h3><p>Sometimes, you want certain logs to <strong>stand out more clearly</strong> — maybe when debugging a critical section or reviewing important system messages.</p><p>That’s where <strong>boxed logging</strong> comes in. It literally draws a box around your log messages.</p><p>We’ll build this using custom formatters that extend colorlog.ColoredFormatter.</p><h3>My favourite logging setup with only the left border:</h3><pre>import logging<br>import logging.config<br>from colorlog import ColoredFormatter<br>import textwrap<br><br>class ShortLogBoxedFormatter(ColoredFormatter):<br>    &quot;&quot;&quot;Custom formatter that wraps log messages inside a box.&quot;&quot;&quot;<br><br>    def format(self, record):<br>        message = super().format(record)  # Get default formatted message<br>        lines = message.splitlines()<br>        width=100<br>        top = &quot;┌&quot; + &quot;─&quot; * (width + 2)<br>        bottom = &quot;└&quot; + &quot;─&quot; * (width + 2) <br>        boxed_lines = [top] + [f&quot;│ {line.ljust(width)} &quot; for line in lines] + [bottom]<br>   <br>        return &quot;\n&quot;.join(boxed_lines)<br><br>class LongLogBoxedFormatter(ColoredFormatter):<br>    &quot;&quot;&quot;Custom formatter that wraps log messages inside a left-box style with word wrapping.&quot;&quot;&quot;<br><br>    # Built-in log attributes we don’t want duplicated as extras<br>    RESERVED = {<br>        &quot;name&quot;, &quot;msg&quot;, &quot;args&quot;, &quot;levelname&quot;, &quot;levelno&quot;, &quot;pathname&quot;,<br>        &quot;filename&quot;, &quot;module&quot;, &quot;exc_info&quot;, &quot;exc_text&quot;, &quot;stack_info&quot;,<br>        &quot;lineno&quot;, &quot;funcName&quot;, &quot;created&quot;, &quot;msecs&quot;, &quot;relativeCreated&quot;,<br>        &quot;thread&quot;, &quot;threadName&quot;, &quot;processName&quot;, &quot;process&quot;, &quot;message&quot;,<br>        &quot;asctime&quot;, &quot;taskName&quot;<br>    }<br><br>    def format(self, record):<br>        message = super().format(record)<br><br>        # Detect and append custom extras<br>        extras = {<br>            k: v for k, v in record.__dict__.items()<br>            if k not in self.RESERVED<br>        }<br>        if extras:<br>            extras_str = &quot; &quot;.join(f&quot;{k}={v}&quot; for k, v in extras.items())<br>            message = f&quot;{message} [{extras_str}]&quot;<br><br>        # Word-wrap long lines<br>        lines = message.splitlines()<br>        width = 100<br>        wrapped_lines = []<br>        for line in lines:<br>            wrapped_lines.extend(<br>                textwrap.wrap(line, width=width, break_long_words=False, break_on_hyphens=False)<br>            )<br><br>        # Draw a left-box style<br>        top = &quot;┌&quot; + &quot;─&quot; * (width + 2)<br>        bottom = &quot;└&quot; + &quot;─&quot; * (width + 2)<br>        boxed_lines = [top] + [f&quot;│ {l}&quot; for l in wrapped_lines] + [bottom]<br><br>        return &quot;\n&quot;.join(boxed_lines)<br><br><br>LOGGING = {<br>    &quot;version&quot;: 1,<br>    &quot;disable_existing_loggers&quot;: False,<br>    &quot;formatters&quot;: {<br>        &quot;boxed&quot;: {<br>            &quot;()&quot;: LongLogBoxedFormatter,  # Direct reference to class, no string path needed<br>            &quot;format&quot;: (<br>                &quot;%(log_color)s%(levelname)-8s%(reset)s &quot;<br>                &quot;%(asctime)s &quot;<br>                &quot;%(blue)s[%(name)s]%(reset)s &quot;<br>                &quot;%(cyan)s%(pathname)s:%(lineno)d%(reset)s - &quot;<br>                &quot;%(message_log_color)s%(message)s%(reset)s&quot;<br>            ),<br>            &quot;datefmt&quot;: &quot;%Y-%m-%d %H:%M:%S&quot;,<br>            &quot;log_colors&quot;: {<br>                &quot;DEBUG&quot;: &quot;cyan&quot;,<br>                &quot;INFO&quot;: &quot;green&quot;,<br>                &quot;WARNING&quot;: &quot;yellow&quot;,<br>                &quot;ERROR&quot;: &quot;red&quot;,<br>                &quot;CRITICAL&quot;: &quot;bold_red&quot;,<br>            },<br>            &quot;secondary_log_colors&quot;: {<br>                &quot;message&quot;: {<br>                    &quot;DEBUG&quot;: &quot;cyan&quot;,<br>                    &quot;INFO&quot;: &quot;green&quot;,<br>                    &quot;WARNING&quot;: &quot;yellow&quot;,<br>                    &quot;ERROR&quot;: &quot;red&quot;,<br>                    &quot;CRITICAL&quot;: &quot;bold_red&quot;,<br>                }<br>            },<br>            &quot;style&quot;: &quot;%&quot;,<br>        },<br>    },<br>    &quot;handlers&quot;: {<br>        &quot;console&quot;: {<br>            &quot;class&quot;: &quot;logging.StreamHandler&quot;,<br>            &quot;formatter&quot;: &quot;boxed&quot;,  # Use boxed formatter<br>            &quot;level&quot;: &quot;DEBUG&quot;,<br>            &quot;stream&quot;: &quot;ext://sys.stdout&quot;,<br>        },<br>    },<br>    &quot;loggers&quot;: {<br>        &quot;&quot;: {&quot;handlers&quot;: [&quot;console&quot;], &quot;level&quot;: &quot;DEBUG&quot;},<br>        &quot;uvicorn.error&quot;: {&quot;level&quot;: &quot;INFO&quot;, &quot;propagate&quot;: True},<br>        &quot;uvicorn.access&quot;: {&quot;handlers&quot;: [&quot;console&quot;], &quot;level&quot;: &quot;INFO&quot;, &quot;propagate&quot;: False},<br>    },<br>}<br><br><br>def setup_logging():<br>    logging.config.dictConfig(LOGGING)<br></pre><h3>2.1 ShortLogBoxedFormatter</h3><p>The ShortLogBoxedFormatter is a custom ColoredFormatter that displays log messages inside a visually distinct box.<br> It has <strong>two variants</strong>:</p><h4>Version 1: Fixed-Width Box (Left Border Only)</h4><p>This version uses a <strong>static width (100 chars)</strong> and only draws the left border.</p><pre>import logging<br>from colorlog import ColoredFormatter<br><br>class ShortLogBoxedFormatter(ColoredFormatter):<br>    &quot;&quot;&quot;Custom formatter that wraps log messages inside a fixed-width box (left border only).&quot;&quot;&quot;<br><br>    def format(self, record):<br>        message = super().format(record)  # Get default formatted message<br>        lines = message.splitlines()<br><br>        width = 100  # fixed width<br>        top = &quot;┌&quot; + &quot;─&quot; * (width + 2)<br>        bottom = &quot;└&quot; + &quot;─&quot; * (width + 2) <br>        boxed_lines = [top] + [f&quot;│ {line.ljust(width)} &quot; for line in lines] + [bottom]<br><br>        return &quot;\n&quot;.join(boxed_lines)</pre><h4>Version 2: Dynamic-Width Box (Full Border)</h4><p>This version <strong>automatically adjusts width</strong> based on the longest log line and adds <strong>both left and right borders</strong>.</p><pre>import logging<br>from colorlog import ColoredFormatter<br><br>class ShortLogBoxedFormatter(ColoredFormatter):<br>    &quot;&quot;&quot;Custom formatter that wraps log messages inside a dynamically sized full box.&quot;&quot;&quot;<br><br>    def format(self, record):<br>        message = super().format(record)  # Get default formatted message<br>        lines = message.splitlines()<br><br>        width = max(len(line) for line in lines)  # dynamic width<br>        top = &quot;┌&quot; + &quot;─&quot; * (width + 2) + &quot;┐&quot;<br>        bottom = &quot;└&quot; + &quot;─&quot; * (width + 2) + &quot;┘&quot;<br>        boxed_lines = [top] + [f&quot;│ {line.ljust(width)} │&quot; for line in lines] + [bottom]<br><br>        return &quot;\n&quot;.join(boxed_lines)</pre><h3>👉 In short:</h3><ul><li><strong>Version 1</strong> → fixed width, simpler, left border only.</li><li><strong>Version 2</strong> → dynamic width, full box (left + right borders).</li></ul><h3>2.2 LongLogBoxedFormatter</h3><p>The previous formatter is best suited for short, single-line messages.<br> But in real-world applications, logs often contain long messages or additional <strong>context fields</strong> (custom extras). Cutting them abruptly at a fixed width isn’t ideal.</p><p>The LongLogBoxedFormatter solves this by:</p><ul><li>Wrapping text at word boundaries (using Python’s textwrap module).</li><li>Preserving readability by avoiding mid-word splits.</li><li>Automatically appending any <strong>extra attributes</strong> passed in the log call (e.g., user_id=123 action=login).</li></ul><pre>import textwrap<br>from colorlog import ColoredFormatter<br><br>class LongLogBoxedFormatter(ColoredFormatter):<br>    &quot;&quot;&quot;Custom formatter that wraps log messages inside a left-box style with word wrapping.&quot;&quot;&quot;<br><br>    # Built-in log attributes we don’t want duplicated as extras<br>    RESERVED = {<br>        &quot;name&quot;, &quot;msg&quot;, &quot;args&quot;, &quot;levelname&quot;, &quot;levelno&quot;, &quot;pathname&quot;,<br>        &quot;filename&quot;, &quot;module&quot;, &quot;exc_info&quot;, &quot;exc_text&quot;, &quot;stack_info&quot;,<br>        &quot;lineno&quot;, &quot;funcName&quot;, &quot;created&quot;, &quot;msecs&quot;, &quot;relativeCreated&quot;,<br>        &quot;thread&quot;, &quot;threadName&quot;, &quot;processName&quot;, &quot;process&quot;, &quot;message&quot;,<br>        &quot;asctime&quot;, &quot;taskName&quot;<br>    }<br><br>    def format(self, record):<br>        message = super().format(record)<br><br>        # Detect and append custom extras<br>        extras = {<br>            k: v for k, v in record.__dict__.items()<br>            if k not in self.RESERVED<br>        }<br>        if extras:<br>            extras_str = &quot; &quot;.join(f&quot;{k}={v}&quot; for k, v in extras.items())<br>            message = f&quot;{message} [{extras_str}]&quot;<br><br>        # Word-wrap long lines<br>        lines = message.splitlines()<br>        width = 100<br>        wrapped_lines = []<br>        for line in lines:<br>            wrapped_lines.extend(<br>                textwrap.wrap(line, width=width, break_long_words=False, break_on_hyphens=False)<br>            )<br><br>        # Draw a left-box style<br>        top = &quot;┌&quot; + &quot;─&quot; * (width + 2)<br>        bottom = &quot;└&quot; + &quot;─&quot; * (width + 2)<br>        boxed_lines = [top] + [f&quot;│ {l}&quot; for l in wrapped_lines] + [bottom]<br><br>        return &quot;\n&quot;.join(boxed_lines)</pre><p>📌 Example output:</p><pre>┌──────────────────────────────────────────────────────────────────────────────────────────────<br>│ INFO     2025-09-08 12:34:56 [main] - This is a long message that wraps neatly<br>│ across multiple lines without breaking words in half. [user_id=42 action=login]<br>└──────────────────────────────────────────────────────────────────────────────────────────────</pre><h3>🎯 When to Use Which?</h3><ul><li><strong>Pure Colored Logging</strong> → Best for general development/debugging. Lightweight, easy to scan.</li><li><strong>ShortLogBoxedFormatter</strong> → Great when you want specific logs (startup messages, configs, critical steps) to pop out.</li><li><strong>LongLogBoxedFormatter</strong> → Ideal when logs may contain <strong>long descriptive text</strong> (e.g., API responses, error traces).</li></ul><h3>🚀 Final Thoughts</h3><p>Good logging is not just about dumping text — it’s about making logs <strong>human-friendly</strong>.<br> Colored logs improve readability, while boxed logs highlight what really matters.<br>By combining both, you can make your Python logs feel like a <strong>dashboard inside your terminal</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e57102a3ec5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>