Skip to content

Latest commit

Β 

History

History
402 lines (313 loc) Β· 11.1 KB

File metadata and controls

402 lines (313 loc) Β· 11.1 KB

CookBook β€” Complete Setup Guide (Zero to Running)


WHAT YOU HAVE (44 files)

cookbook/
β”œβ”€β”€ README.md
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ .env                          ← DB URI, JWT secret, port
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ server.js                     ← Entry point
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── db.js
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ auth.controller.js        ← register, login, getMe
β”‚   β”‚   β”œβ”€β”€ recipe.controller.js      ← CRUD + search + pagination
β”‚   β”‚   β”œβ”€β”€ favorite.controller.js    ← save/unsave/list
β”‚   β”‚   └── user.controller.js        ← profile, change password
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── auth.middleware.js        ← JWT protect middleware
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ User.js
β”‚   β”‚   β”œβ”€β”€ Recipe.js
β”‚   β”‚   └── Favorite.js
β”‚   └── routes/
β”‚       β”œβ”€β”€ auth.routes.js
β”‚       β”œβ”€β”€ recipe.routes.js
β”‚       β”œβ”€β”€ favorite.routes.js
β”‚       └── user.routes.js
β”‚
└── frontend/
    β”œβ”€β”€ index.html
    β”œβ”€β”€ package.json
    β”œβ”€β”€ vite.config.js
    β”œβ”€β”€ tailwind.config.js
    β”œβ”€β”€ postcss.config.js
    └── src/
        β”œβ”€β”€ main.jsx
        β”œβ”€β”€ App.jsx
        β”œβ”€β”€ index.css
        β”œβ”€β”€ context/
        β”‚   └── AuthContext.jsx       ← Global login state
        β”œβ”€β”€ services/
        β”‚   β”œβ”€β”€ api.js                ← Axios instance with JWT
        β”‚   └── recipeService.js      ← All API calls
        β”œβ”€β”€ routes/
        β”‚   └── PrivateRoute.jsx      ← Protects auth-only pages
        β”œβ”€β”€ components/
        β”‚   β”œβ”€β”€ Navbar.jsx
        β”‚   β”œβ”€β”€ RecipeCard.jsx
        β”‚   β”œβ”€β”€ RecipeForm.jsx        ← Shared by Create + Edit
        β”‚   β”œβ”€β”€ SearchBar.jsx
        β”‚   └── Spinner.jsx
        └── pages/
            β”œβ”€β”€ Home.jsx              ← Search, filter, browse
            β”œβ”€β”€ Login.jsx
            β”œβ”€β”€ Register.jsx
            β”œβ”€β”€ RecipeDetail.jsx      ← Full recipe + ingredient checklist
            β”œβ”€β”€ CreateRecipe.jsx
            β”œβ”€β”€ EditRecipe.jsx
            β”œβ”€β”€ Favorites.jsx
            β”œβ”€β”€ MyRecipes.jsx
            β”œβ”€β”€ Profile.jsx           ← Edit profile + change password
            └── NotFound.jsx

STEP 1 β€” INSTALL NODE.JS (if not installed)

  1. Go to https://nodejs.org
  2. Download the LTS version (green button)
  3. Install it (click Next through the installer)
  4. Open a terminal and verify:
node -v
npm -v

You should see version numbers. If you do, move on.


STEP 2 β€” INSTALL MONGODB (Local)

Option A β€” Local MongoDB (Recommended for development)

  1. Go to https://www.mongodb.com/try/download/community
  2. Select: Version = current, Platform = Windows/Mac, Package = MSI
  3. Download and install (use "Complete" setup)
  4. During install, check "Install MongoDB as a Service" β€” this means it starts automatically
  5. Also install MongoDB Compass when prompted (it's the GUI, very useful)

To verify MongoDB is running, open terminal and type:

mongosh

If you see a > prompt, MongoDB is running. Type exit to quit.

Option B β€” MongoDB Atlas (Cloud, no install needed)

  1. Go to https://www.mongodb.com/atlas
  2. Create a free account
  3. Create a free cluster (M0 Sandbox β€” free forever)
  4. Click "Connect" β†’ "Drivers" β†’ copy the connection string
  5. It looks like: mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/
  6. Replace the MONGO_URI in your .env with this string + add cookbook at the end: mongodb+srv://username:password@cluster0.xxxxx.mongodb.net/cookbook

STEP 3 β€” INSTALL VS CODE (if not installed)

  1. Go to https://code.visualstudio.com
  2. Download and install
  3. Recommended extensions to install inside VS Code:
    • ES7+ React/Redux/React-Native snippets
    • Tailwind CSS IntelliSense
    • Prettier - Code formatter
    • Thunder Client (for API testing, alternative to Postman)

STEP 4 β€” SET UP THE PROJECT

4.1 β€” Copy the project folder

Take the cookbook folder you downloaded and place it somewhere clean, for example:

C:\Users\YourName\Projects\cookbook       (Windows)
/Users/YourName/Projects/cookbook         (Mac)

Open VS Code β†’ File β†’ Open Folder β†’ select the cookbook folder.

You should see both backend/ and frontend/ folders in the sidebar.


4.2 β€” Set up the Backend

Open the integrated terminal in VS Code: press Ctrl + ` (backtick) or go to Terminal β†’ New Terminal.

cd backend
npm install

This installs: express, mongoose, jsonwebtoken, bcryptjs, cors, dotenv, nodemon, express-validator.

Wait for it to finish. You'll see a node_modules folder appear inside backend/.

Edit the .env file (open backend/.env):

PORT=5000
MONGO_URI=mongodb://localhost:27017/cookbook
JWT_SECRET=mySuperSecretKey2024CookBook
JWT_EXPIRES_IN=7d
NODE_ENV=development
  • If using local MongoDB: keep MONGO_URI as is
  • If using Atlas: replace MONGO_URI with your Atlas connection string
  • Change JWT_SECRET to any long random string (keep it secret)

4.3 β€” Set up the Frontend

Open a second terminal in VS Code (click the + icon in the terminal panel):

cd frontend
npm install

This installs: react, react-dom, react-router-dom, axios, react-icons, react-toastify, tailwindcss, vite, etc.

Wait for it to finish.


STEP 5 β€” RUN THE PROJECT

You need two terminals running at the same time.

Terminal 1 β€” Backend:

cd backend
npm run dev

You should see:

βœ… MongoDB connected
πŸš€ Server running on port 5000

If you see an error about MongoDB connection, make sure MongoDB service is running (see Step 2).

Terminal 2 β€” Frontend:

cd frontend
npm run dev

You should see:

  VITE v5.x.x  ready in xxx ms

  ➜  Local:   http://localhost:5173/

STEP 6 β€” OPEN THE APP

Open your browser and go to:

http://localhost:5173

You'll see the CookBook homepage with a search bar and recipe cards.


STEP 7 β€” TEST THE APP

Register an account:

  1. Click "Get Started" in the navbar
  2. Fill in name, email, password (min 6 chars)
  3. You'll be redirected to the home page, logged in

Create a recipe:

  1. Click "+ New Recipe" in the navbar
  2. Fill in all fields β€” title, cuisine, cooking time, ingredients, steps
  3. Click "Publish Recipe"
  4. You'll be redirected to the recipe detail page

Save a recipe:

  1. Browse the home page
  2. Click the heart icon on any recipe card OR open a recipe and click "Save Recipe"

View saved recipes:

  1. Click "Saved" in the navbar

Edit/Delete your recipe:

  1. Go to "My Recipes" in the navbar
  2. Use the eye/edit/delete icons on each row

Profile:

  1. Click your name in the navbar β†’ Profile
  2. Update your bio or change your password

STEP 8 β€” TESTING THE API WITH POSTMAN (Optional but good practice)

Install Postman: https://www.postman.com/downloads/

Register:

  • Method: POST
  • URL: http://localhost:5000/api/auth/register
  • Body β†’ raw β†’ JSON:
{
  "name": "Test User",
  "email": "test@example.com",
  "password": "123456"
}

Login:

  • Method: POST
  • URL: http://localhost:5000/api/auth/login
  • Body β†’ raw β†’ JSON:
{
  "email": "test@example.com",
  "password": "123456"
}

Copy the token from the response.

For protected routes (any route that needs auth):

  • Go to Headers tab
  • Add: Authorization β†’ Bearer YOUR_TOKEN_HERE

Create a recipe:

  • Method: POST
  • URL: http://localhost:5000/api/recipes
  • Header: Authorization: Bearer YOUR_TOKEN
  • Body β†’ raw β†’ JSON:
{
  "title": "Butter Chicken",
  "cuisine": "Indian",
  "difficulty": "Medium",
  "cookingTime": 45,
  "servings": 4,
  "description": "Creamy and delicious",
  "ingredients": [
    { "name": "Chicken", "quantity": "500g" },
    { "name": "Butter", "quantity": "2 tbsp" }
  ],
  "steps": [
    { "stepNumber": 1, "instruction": "Marinate the chicken" },
    { "stepNumber": 2, "instruction": "Cook in sauce" }
  ],
  "tags": ["chicken", "indian", "creamy"]
}

COMMON ERRORS & FIXES

"MongoDB connection error" or "ECONNREFUSED"

MongoDB is not running. Fix:

  • Windows: Press Win+R β†’ type services.msc β†’ find "MongoDB" β†’ right click β†’ Start
  • Mac: Run brew services start mongodb-community in terminal
  • Or use MongoDB Atlas instead (cloud, always on)

"Cannot find module" errors

You forgot to run npm install. Go into that folder and run it again.

Frontend shows blank page or errors in console

Open browser console (F12) and check the error. Usually it's:

  • Backend not running β†’ start npm run dev in backend folder
  • Wrong API URL β†’ check vite.config.js proxy is pointing to port 5000

"Port already in use"

Another process is using port 5000 or 5173. Either kill it or change the PORT in .env.

CSS not loading / Tailwind not working

Run this inside the frontend folder:

npm install tailwindcss postcss autoprefixer

JWT token errors (401 Unauthorized)

  • You're not logged in, or your token expired
  • Log out and log in again
  • Make sure JWT_SECRET in .env hasn't changed

ALL API ROUTES REFERENCE

POST   /api/auth/register          Register new user
POST   /api/auth/login             Login, returns JWT token
GET    /api/auth/me                Get current user (auth required)

GET    /api/recipes                Get all recipes
                                   Query params:
                                   ?search=chicken
                                   ?cuisine=Indian
                                   ?difficulty=Easy
                                   ?maxTime=30
                                   ?page=1&limit=9

GET    /api/recipes/my-recipes     Get logged-in user's recipes (auth)
GET    /api/recipes/:id            Get single recipe by ID
POST   /api/recipes                Create recipe (auth)
PUT    /api/recipes/:id            Update recipe (auth + owner only)
DELETE /api/recipes/:id            Delete recipe (auth + owner only)

GET    /api/favorites              Get saved recipes (auth)
POST   /api/favorites/:recipeId    Save a recipe (auth)
DELETE /api/favorites/:recipeId    Unsave a recipe (auth)

GET    /api/users/profile          Get profile (auth)
PUT    /api/users/profile          Update name/bio (auth)
PUT    /api/users/change-password  Change password (auth)

MONGODB COMPASS (GUI to see your data)

  1. Open MongoDB Compass
  2. Connect to: mongodb://localhost:27017
  3. You'll see a cookbook database appear after you register/create recipes
  4. Click it to see: users, recipes, favorites collections
  5. You can view, edit, delete documents directly from here β€” great for debugging

QUICK RECAP β€” WHAT TO RUN EVERY TIME

  1. Make sure MongoDB is running (local) or Atlas is connected
  2. Open VS Code in the cookbook folder
  3. Terminal 1: cd backend && npm run dev
  4. Terminal 2: cd frontend && npm run dev
  5. Open browser: http://localhost:5173

That's it. Both terminals must be running at the same time.