Personal Catalogue: React, Express & MongoDB — Chapter 1, Exercise 2 ===================================================================== TASK Set up the Express backend and confirm a real MongoDB connection via the console log, then explain in one sentence what Mongoose discriminators are for and why they matter for this project specifically. SOLUTION Setup: 1. `mkdir catalogue-api && cd catalogue-api` 2. `npm init -y` 3. `npm install express mongoose cors dotenv` 4. `npm install --save-dev nodemon` 5. Create `.env` with a real `MONGODB_URI` (a local `mongodb://localhost:27017/catalogue` or a MongoDB Atlas connection string) and a `PORT`. 6. Create `server.js` with the Express app, `mongoose.connect(process.env.MONGODB_URI)`, and `app.listen(...)`. 7. Run `npx nodemon server.js`. Run: Output: MongoDB connected Server running on port 4000 Both lines confirm the real setup: the first line proves the connection string in `.env` actually reaches a running MongoDB instance; the second proves Express itself started and is listening for requests. One-sentence explanation: Mongoose discriminators let several genuinely different document types (a book, a CD, a DVD) share one single MongoDB collection while each still gets validated against its own separate schema, distinguished by a stored discriminator field — which matters here because this project's own four item types have very different real fields and this course is deliberately avoiding either one bloated table or four separate collections that would need joining back together. WHY THIS WORKS AS AN ANSWER ---------------------------- It walks through the actual setup commands in the order they need to run, shows the real console output that proves the connection worked rather than just claiming it did, and gives a precise, single-sentence definition of discriminators tied directly to this project's own real motivation for using them — not a generic MongoDB feature description.