A backend (or "server-side") application handles tasks like:
- Storing and retrieving data from databases
- User authentication and authorization
- Business logic processing
- API endpoints that client applications can communicate with
- User registration with unique user IDs
- Secure login and authentication
- Health check endpoint for monitoring
- Password hashing for security
If you're new to Node.js development, follow these steps(If you have installed firebase, you can skip this section as firebase requires Node.js):
- Install Node.js: Download and install from nodejs.org
- Verify installation:
node -v # Shows Node.js version npm -v # Shows npm (Node Package Manager) version
- Understanding package.json: This file lists project dependencies and scripts
- Node modules: Libraries installed via npm are stored in the "node_modules" folder
- Node.js (v14+ recommended)
- MongoDB instance
-
Download the zip file from the repository and extract it to your desired location. Open it in VS Code or your preferred code editor.
-
Install dependencies
npm install- Environment Configuration
Create a
.envfile in the root directory with the following variables:
PORT=3000 # The port your server will run on
MONGODB_URI=<your connection_string> # Connection to MongoDB
JWT_SECRET=<some_random_string> # Secret key for JWT authentication
- Start the development server
npm run devThis will start the server using nodemon, which automatically restarts the server when you make changes to the code.
/
├── index.js # Application entry point - starts the server
├── src/ # Source code directory
│ ├── app.js # Express setup - configures the web server
│ ├── routes/ # API route definitions - maps URLs to controllers
│ │ ├── auth.route.js # Authentication routes (login, register)
│ │ └── healthcheck.route.js # Health check routes for monitoring
│ ├── controllers/ # Request handlers - business logic for each route
│ │ ├── login.controller.js # Handles user login
│ │ ├── register.controller.js # Handles user registration
│ │ └── healthcheck.controller.js # Handles health checks
│ ├── models/ # Database schemas - defines data structure
│ │ └── User.js # User model - defines user properties
│ └── db/
│ └── connectDB.js # Database connection - connects to MongoDB
└── .env # Environment variables (not in repository)
-
index.js: The starting point of the application. It loads environment variables, connects to the database, and starts the server.
-
app.js: Creates and configures the Express application, setting up middleware for parsing requests.
-
routes/auth.route.js: Defines the URLs for authentication operations and connects them to controller functions.
-
controllers/login.controller.js: Contains the logic for authenticating users, comparing passwords, and updating login state.
-
models/User.js: Defines the structure of user data in the database using Mongoose schema.
This API uses these HTTP methods:
- GET: Retrieve data (like health check)
- POST: Create or update data (like register, login)
HTTP status codes in responses:
- 200: Success (OK)
- 201: Created successfully
- 400: Bad request (client error)
- 404: Not found
- 500: Server error
-
Password Hashing: Passwords are never stored as plain text. We use bcrypt which:
- Adds random "salt" to make each hash unique
- Uses one-way encryption that can't be reversed
-
Input Validation: All user inputs are checked to:
- Prevent malicious data
- Ensure required fields are provided
- Reject invalid formats
-
JWT Authentication: JSON Web Tokens (JWT) are used to securely identify users after login. They:
- Contain user information and a signature
- Are sent in the Authorization header for protected routes
- Allow stateless authentication (no session storage needed)
-
Environment Variables: Sensitive information like database credentials are kept in .env files that aren't committed to repositories.
If you see "Error connecting to MongoDB", check:
- Is MongoDB running?
- Is your MONGODB_URI correct in the .env file?
- Do you have network access to the database?
Run npm install to make sure all dependencies are installed.
- Check if another application is using the same port
- Verify that your .env file exists with the correct variables
To run in development mode with nodemon (automatically restarts when code changes):
npm run devFor this assignment, you will extend this authentication backend to create a basic expense tracker application. This will help you learn how to build upon an existing codebase by adding new models, controllers, and routes.
Building on the existing authentication system, implement the following features:
- Expense Management
- Create new expenses with amount, description, category, and date
- View all expenses for the logged-in user
- Update existing expenses
File path: src/models/Expense.js
Create an Expense model that includes:
- userId (to connect expenses to a specific user)
- amount (the cost of the expense)
- description (what the expense was for)
- category (like "Food", "Transportation", etc.)
- date (when the expense occurred)
File path: src/controllers/expense.controller.js
Create an expense controller with these functions:
createExpense: Add a new expense to the databasegetExpenses: Retrieve all expenses for a specific userupdateExpense: Update an existing expense by its ID
File path: src/routes/expense.route.js
Create routes that define these API endpoints:
POST /api/expense/create: Create a new expenseGET /api/expense/all: Fetch all expenses for a userPUT /api/expense/update: Update an expense by ID
File to modify: index.js
Add your new expense routes to the main application by importing your expense router and adding the appropriate app.use() statement.
When you're done, your project structure should look like this:
/
├── index.js # Application entry point - starts the server
├── src/ # Source code directory
│ ├── app.js # Express application setup - configures the web server
│ ├── routes/ # API route definitions - maps URLs to controllers
│ │ ├── auth.route.js # Authentication routes (login, register)
│ │ └── healthcheck.route.js # Health check routes for monitoring
│ │ ├── expense.route.js # Expense management routes (create, get, update)
│ ├── controllers/ # Request handlers - business logic for each route
│ │ ├── login.controller.js # Handles user login
│ │ ├── register.controller.js # Handles user registration
│ │ └── healthcheck.controller.js # Handles health checks
│ │ ├── expense.controller.js # Handles expense management
│ ├── models/ # Database schemas - defines data structure
│ │ └── User.js # User model - defines user properties
│ │ └── Expense.js # Expense model - defines expense properties
│ └── db/
│ └── connectDB.js # Database connection - connects to MongoDB
└── .env # Environment variables (not in repository)