One feature, every layer: finishing a review endpoint from Express to React
Browsing worked. Posting a review returned 501 and the form showed placeholder text. Finishing that one feature meant touching the database, the API and the UI — and fixing a bug that stopped the server from starting at all.
The Book Review Platform lets readers browse a catalog of books, open a book's details and leave a review. In the version I started from, two of those three worked. The API route for posting a review returned 501 Not Implemented, and the React form showed a note telling the developer to implement it.
Small features that cross every layer are useful precisely because nothing can be faked. The route has to find the right document, the update has to be correct under concurrency, the response has to be honest about what happened, and the UI has to reflect it without a page reload.
Finding the right book
The first decision is the one that silently breaks everything if you get it wrong. The book ID arrives in the URL as a string, but MongoDB stores _id as an ObjectId. Wrapping the parameter in new ObjectId() makes the types match. Skip it and every lookup fails without erroring — the query is valid, it just never matches anything.
Appending without a read-modify-write race
The obvious way to add a review is to read the book, push onto the array in JavaScript, and write the document back. That is also the way to lose reviews: two submissions arriving close together each read the same array, and the second write overwrites the first.
So the update uses $push, which appends to the array in a single atomic operation on the server. No read, no window, no lost review. It is one line of difference and it removes an entire class of bug.
Reporting what actually happened
The route then has to decide what to tell the client, and the question it needs to answer is "does this book exist?" The update result carries matchedCount, which answers exactly that — and it is the right field rather than a modified count, because any match is always modified by a push. Zero matches means no book has that ID, so the API returns 404. Otherwise it returns 201.
One detail with teeth: the route closes its database connection immediately after responding, so the update has to be awaited. Without the await, the connection can close while the write is still in flight.
Updating the screen without asking the server again
On the frontend, the handler blocks the browser's default form submission, rejects empty input before making any network call, and posts the review to the endpoint for the selected book. The interesting part is what happens after the server confirms.
Rather than refetching the whole book, it appends the review to component state and clears the input. That saves a round trip, but it only works if the update is immutable: a new object and a new array, rather than pushing onto the existing one. React detects the change by reference, so mutating the array in place would leave the screen stale even though the data had changed.
Every failure is caught and reported to the user instead of breaking the page, and the status message under the form tells them plainly which happened.
The bug that stopped the server starting
None of that could be tested at first, because npm start crashed immediately in the backend with an error saying Node couldn't find a module named seed.js; — semicolon included. With no API running, the frontend loaded and showed an empty book list.
The start script was node seed.js; nodemon index.js. On macOS and Linux, npm runs scripts through a POSIX shell, where the semicolon separates two commands. On Windows, npm uses cmd.exe, which doesn't treat it that way, so Node received seed.js; as the filename.
The semicolon in that error message is the whole diagnosis. It is not a missing file; it is a file whose name includes punctuation that was supposed to be a separator.
The fix is to use &&, which works in every shell. It also adds a safety property worth more than the portability: nodemon now starts only if seeding succeeded, so the API never runs against a database that failed to initialize.
Testing the paths that aren't the happy one
Five scenarios, not one. A syntax check on the route file. A valid review, confirming a 201 and the review stored in the book's array. A request against an unknown ID, confirming 404 rather than a silent success. A submission with no review content, confirming the existing 400 validation still held after my changes. And the end-to-end path in the browser.
The third and fourth are the ones that matter. It is easy to write a route that works for well-formed input and returns something misleading for everything else, and a 404 that quietly returns 201 is exactly the kind of defect that surfaces months later as "some reviews just don't save."
What I'd improve next
Three things stand out, and the first is a real gap rather than a polish item. A malformed ID makes new ObjectId() throw, which surfaces to the user as a 500 — so validating the ID and returning a helpful 400 is the first change I'd make. Second, every request currently opens and closes its own MongoDB client; connecting once at startup and sharing the pooled client would cut latency, and closing in a finally block would prevent leaks when an error is thrown. Third, reviews are stored as bare strings, so storing them as objects with text, rating, author and timestamp — and returning the created review from the 201 — would make the API considerably more useful.
Beyond that: move the database URL and API base URL into environment variables instead of hard-coding localhost, and add API tests for the 201, 400 and 404 paths plus a form test, run in CI.
Node.js · Express · MongoDB · React 18 · axios · REST · nodemon · Webpack 5
Read the full engineering dossier →
The full technical report is available on request. More of my work is at esmeraldaspace.com, and I am reachable on LinkedIn.