This is the backend API for the Happy Thoughts project, used to store and manage "happy thoughts" submitted by users. It is built with Node.js, Express, and MongoDB, hosted on Render.
- GET /thoughts: Fetches the 20 most recent happy thoughts, sorted by creation date.
- POST /thoughts: Allows users to submit a new happy thought.
- POST /thoughts/:thoughtId/like: Adds a like (heart) to a specific thought by ID.
This backend is connected to the Happy Thoughts Frontend, a React application for displaying and interacting with the happy thoughts.
- Backend Repository: Happy Thoughts API
- Backend URL: https://project-happy-thoughts-api-gns9.onrender.com
When I started integrating my React frontend, Node.js backend, and MongoDB Atlas database, I faced several issues. Here's a few of them:
- My MongoDB Atlas user seemed to have the correct password, but for some non-obvious reason, the connection still failed. I resolved this by generating a new password for the user and updating the
MONGO_URLin Render’s environment variables. - During development, I got an error saying that
savedThoughtwas not defined in thePOST /thoughtsendpoint. This was caused by a typo, and I fixed it by correctly referencing the newly created thought object asnewThought. - The error 'Operation 'happythoughts.find()' buffering timed out' because the database wasn’t properly configured in MongoDB Atlas. This was resolved by adding 0.0.0.0/0 in Network Access in MongoDB Atlas to allow connections from any IP address (necessary for Render).
I used Postman a lot to test my API endpoints and MongoDB Compass to confirm that data was being stored in the database. I uploaded a json with a few "happy thoughts" to make the testing easier, while debugging issues with my initial POST request.
I also sketched a simple flow on paper to map out how each part of the application interacts, such as how MONGO_URL is used in the backend to connect to MongoDB Atlas, and that BASE_URL links the frontend to the backend. This really helped when debugging.
- Frontend URL: Project Happy Thoughts
- Backend URL: https://project-happy-thoughts-api-gns9.onrender.com