πŸ“… Day 2 Guide: Setting Up the Backend for Our MERN Stack Expense Tracker

Vishal GuptaVishal Gupta
3 min read

Welcome back to Day 2 of building our Expense Tracker Web App from scratch using the MERN Stack! Yesterday, we laid the foundation for the frontend using React and Vite, with a clean folder structure and routing system.

Today, we shift gears to the backend setup β€” bringing in Node.js, Express.js, and MongoDB to power our server, APIs, and database integration. Let's dive right in! πŸš€


πŸ”§ Backend Architecture Overview

To maintain scalability and clean separation of concerns, we’ve structured our backend thoughtfully:

πŸ“ Folder Structure (Backend)

bashCopyEditbackend/
β”œβ”€β”€ configs/           # Database connection and environment configs
β”‚   └── db.js
β”œβ”€β”€ controllers/       # Logic for handling routes
β”‚   β”œβ”€β”€ auth.js
β”‚   β”œβ”€β”€ dashboard.js
β”‚   β”œβ”€β”€ expense.js
β”‚   └── income.js
β”œβ”€β”€ models/            # MongoDB schemas/models
β”‚   β”œβ”€β”€ User.js
β”‚   β”œβ”€β”€ Expense.js
β”‚   └── Income.js
β”œβ”€β”€ routes/            # API route definitions
β”‚   β”œβ”€β”€ authRoutes.js
β”‚   β”œβ”€β”€ dashboardRoutes.js
β”‚   β”œβ”€β”€ expenseRoutes.js
β”‚   └── incomeRoutes.js
β”œβ”€β”€ middlewares/       # Custom middleware
β”‚   β”œβ”€β”€ authMiddleware.js
β”‚   └── uploadMiddleware.js
β”œβ”€β”€ uploads/           # Stores profile images for users
└── server.js          # Entry point for the Node.js server

βš™οΈ Backend Technologies Used

  • Node.js – JavaScript runtime for the backend.

  • Express.js – Fast, minimalist web framework.

  • MongoDB – NoSQL database to store expenses, income, and user profiles.

  • Mongoose – ODM to interact with MongoDB using schemas and models.

  • dotenv – Manages environment variables securely.

  • CORS – Cross-Origin Resource Sharing configuration.

  • Multer (coming soon) – For handling profile image uploads.


πŸ“‚ Key Server File: server.js

Here’s a breakdown of the main entry file:

jsCopyEditrequire("dotenv").config();
const express = require("express");
const cors = require("cors");
const path = require("path");
const connectDB = require("./configs/db");

// Import Routes
const authRoutes = require("./routes/authRoutes");
const dashboardRoutes = require("./routes/dashboardRoutes");
const expenseRoutes = require("./routes/expenseRoutes");
const incomeRoutes = require("./routes/incomeRoutes");

const app = express();

// CORS Setup
app.use(
  cors({
    origin: process.env.CLIENT_URL || "*",
    methods: ["GET", "POST", "PUT", "DELETE"],
    allowedHeaders: ["Content-Type", "Authorization"],
  })
);

// Parse incoming requests
app.use(express.json());

// Connect to MongoDB
connectDB();

// API Routes
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/dashboard", dashboardRoutes);
app.use("/api/v1/expense", expenseRoutes);
app.use("/api/v1/income", incomeRoutes);

// Serve user-uploaded profile pictures
app.use("/uploads", express.static(path.join(__dirname, "uploads")));

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`Server is running at port ${PORT}`);
});

πŸ” Today’s Steps

Now that we have the backend structure and server up and running, here’s what’s I have done in today’s time:

  • Implementing authentication using JWT.

  • Building and connecting MongoDB models for users, income, and expense.

  • Creating secure APIs for user registration, login, adding/removing expenses and incomes.

  • Setting up profile image uploads.

  • Integrating frontend and backend with protected routes.


πŸ‘¨β€πŸ’» Developer’s Note

Organizing your codebase from day one makes scaling and debugging significantly easier. By separating routes, controllers, and middleware, we’re keeping things modular, maintainable, and clean β€” which every developer will appreciate in the long run.


πŸ“Œ Recap

βœ… Set up the frontend with React + Vite
βœ… Established a tidy folder structure for both the frontend and backend
βœ… Installed essential backend tools: Node, Express, MongoDB
βœ… Connected the server to the database
βœ… Built a modular backend architecture for API endpoints and future features


Source Code - Day02

  • The source code for Day 2 of this project will be available on GitHub, where you can explore the detailed implementation of the features we discussed. You will find all the code necessary to understand how we set up the backend structure, implemented authentication, and connected our MongoDB models.

    GitHub - https://github.com/vishalgupta-02

  • Additionally, I am maintaining comprehensive documentation for this project to ensure better access and management of all the components involved. This documentation will provide step-by-step guidance and insights into the development process, making it easier for you to follow along and replicate the setup if needed.

    Documentation Link - https://expense-tracker-web.hashnode.space/

πŸ’¬ Follow along tomorrow (Day 3) as we dive into building more stuffs required to run the expense tracker app!

0
Subscribe to my newsletter

Read articles from Vishal Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Gupta
Vishal Gupta

Hi, I'm Vishal Gupta, a passionate and self-driven Full-Stack Developer with a strong command of the MERN stack (MongoDB, Express.js, React.js, Node.js). I love turning complex problems into clean, scalable solutions through elegant code and user-friendly interfaces. With hands-on experience building real-world applications like MedEase, TripGenius, and now this Expense Tracker, I strive to build software that’s not only functional but also impactful. Whether it's creating secure authentication systems, responsive UI components, or integrating AI-powered features, I aim to deliver value with every line of code. πŸ”§ Key Skills: Frontend: React.js, Vite, Tailwind CSS, ShadCN, Context API Backend: Node.js, Express.js, MongoDB, JWT, REST APIs Dev Tools: Git & GitHub, Postman, Firebase, Vercel Soft Skills: Problem-solving, communication, and a strong desire to learn & grow I believe in continuous learning and building meaningful products that solve real-life problems. Currently, I’m open to opportunities where I can collaborate, innovate, and contribute to exciting web development projects.