Imagine you're helping build a simple student project tracker for a school. Your task is to set up the backend using Node.js and Express.js, and connect it to a MongoDB database using Mongoose.
This assignment gives you mentor-supported, hands-on practice with writing the connection logic, using environment variables, and verifying whether your Express server is successfully connected to the MongoDB database.
-
Start with the provided Express boilerplate project.
-
Install the dependencies using:
npm install
-
Create a
.env
file in the root directory of the project. -
Add the MongoDB connection string in the
.env
file as shown below:MONGO_URI=mongodb://localhost:27017/studentProjectsDB
-
Using environment variables helps keep sensitive information separate from your code.
-
Open
index.js
in your project. -
Make sure the Express server is set up.
-
Use Mongoose to:
-
Load the MongoDB connection string from the
.env
file. -
Connect to the
studentProjectsDB
database. -
Log a message based on whether the connection is successful or fails:
- Success:
Connected to MongoDB
- Failure:
Error connecting to MongoDB: <error details>
- Success:
-
- Start the server using
node index.js
ornpm start
. - Check the terminal output to confirm that the database connection message is displayed.
- A StackBlitz starter link will be provided for this assignment.
- Open the link and click Fork to create your own working copy.
You can either push manually or directly from StackBlitz.
-
Download your project files from StackBlitz.
-
Use your terminal to run the following commands:
git init git remote add origin <your_github_repo_url> git add . git commit -m "Completed student project tracker DB connection" git push -u origin main
- Use StackBlitz’s Push to GitHub feature to upload your work.
-
Once your code is on GitHub:
- Copy the GitHub repository URL.
- Submit the link through the assignment submission form.
https://github.com/<your_username>/<repository_name>
If your GitHub username is anikaDev
and the repository is student-tracker-backend
, submit:
https://github.com/anikaDev/student-tracker-backend