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

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!
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.