Backend API for the Expense Tracker application. This service handles user authentication, profile management, expenses, incomes, budgets, and MongoDB persistence for the frontend app.
Frontend repository: https://github.com/devMonkRahul/Expense-Tracker-Frontend
- User registration and login
- JWT-based protected routes
- Profile fetch and profile update endpoints
- Income CRUD with date-based filtering and pagination
- Expense CRUD with date-based filtering and pagination
- Budget CRUD
- MongoDB persistence with Mongoose
- Security middleware with
helmet,cors, and request logging viamorgan
- Node.js
- Express.js
- MongoDB
- Mongoose
- JWT Authentication
- Docker and Docker Compose
.
├── app.js
├── Dockerfile
├── docker-compose.yaml
├── server
│ ├── config
│ ├── constants.js
│ ├── controllers
│ ├── middlewares
│ ├── models
│ ├── routes
│ └── utils
└── logs
Base URL: http://localhost:4000
GET /- Server health checkGET /logs- Returns the last 100 access log lines
POST /api/v1/user/registerPOST /api/v1/user/loginGET /api/v1/user/profilePATCH /api/v1/user/profile/updatePATCH /api/v1/user/profile/changePasswordGET /api/v1/user/validate/username/:usernameGET /api/v1/user/validate/email/:email
POST /api/v1/transaction/addIncomeGET /api/v1/transaction/getIncomesPATCH /api/v1/transaction/editIncome/:incomeIdDELETE /api/v1/transaction/deleteIncome/:incomeIdPOST /api/v1/transaction/addExpenseGET /api/v1/transaction/getExpensesPATCH /api/v1/transaction/editExpense/:expenseIdDELETE /api/v1/transaction/deleteExpense/:expenseId
POST /api/v1/budget/addBudgetGET /api/v1/budget/getBudgetsGET /api/v1/budget/getBudget/:budgetIdPATCH /api/v1/budget/updateBudget/:budgetIdDELETE /api/v1/budget/deleteBudget/:budgetId
Most application routes require an Authorization header in the format Bearer <token>.
Create a .env file in the project root. You can copy the example file:
cp .env.example .envRequired variables:
PORT=4000
MONGODB_URI=mongodb://localhost:27017/expense-tracker
ACCESS_TOKEN_SECRET=replace-with-a-strong-secret
ACCESS_TOKEN_EXPIRY=1d- Node.js 20 or newer
- npm
- MongoDB running locally or a MongoDB Atlas connection string
- Clone the repository:
git clone https://github.com/devMonkRahul/Expense-Tracker-Backend.git
cd Expense-Tracker-Backend- Install dependencies:
npm install- Create your environment file:
cp .env.example .env-
Update
MONGODB_URIandACCESS_TOKEN_SECRETin.env. -
Start the development server:
npm run devThe API will be available at http://localhost:4000.
This project includes a docker-compose.yaml that starts:
mongodbon port27017expense-tracker-backendon port4000
Run:
docker compose up --buildTo stop the services:
docker compose downTo stop and remove the MongoDB volume as well:
docker compose down -vBuild the image:
docker build -t expense-tracker-backend .Run the container:
docker run -p 4000:4000 \
-e PORT=4000 \
-e MONGODB_URI=mongodb://host.docker.internal:27017/expense-tracker \
-e ACCESS_TOKEN_SECRET=replace-with-a-strong-secret \
-e ACCESS_TOKEN_EXPIRY=1d \
expense-tracker-backendIf you are running Docker on Linux and host.docker.internal is unavailable, replace it with a reachable MongoDB host or use Docker Compose.
Frontend repository: https://github.com/devMonkRahul/Expense-Tracker-Frontend
For local integration:
- Start this backend on
http://localhost:4000 - Start the frontend separately
- Point the frontend API configuration to this backend URL
The backend CORS configuration already allows:
http://localhost:5173http://localhost:5174https://expense-tracker.therahul.xyzhttps://expense-tracker-mu-wheat.vercel.app
npm run dev- Start the app withnodemonnpm start- Start the app in production mode
- Access logs are written to
logs/access.log - The Docker image runs the app as a non-root user
- Docker Compose uses a MongoDB named volume for persistence