Skip to content

AkhilRajSri/server_interview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Food Ordering Mock Server

Mock Express server for food ordering interview problem. Uses data.json for persistence without a database.

Installation

npm install

Running the Server

# Development (with auto-reload)
npm run dev

# Production
npm start

Server runs on http://localhost:3001

Features

  • ✅ Menu items with branch-specific availability
  • ✅ Multiple branch support
  • ✅ Real-time availability changes (auto-polling simulation)
  • ✅ Simple, clean API with only 2 endpoints
  • ✅ CORS enabled for frontend integration

API Endpoints

1. Get All Branches

GET /api/branches

Response:

{
  "success": true,
  "data": [
    {
      "id": "branch_1",
      "name": "Downtown Branch",
      "address": "123 Main St"
    },
    {
      "id": "branch_2",
      "name": "Uptown Branch",
      "address": "456 Park Ave"
    }
  ],
  "timestamp": 1234567890
}

2. Get Menu Items for a Branch

GET /api/branches/:branchId/menu

Parameters:

  • branchId (path parameter): The ID of the branch (e.g., "branch_1")

Response:

{
  "success": true,
  "data": [
    {
      "id": "item_1",
      "name": "Margherita Pizza",
      "description": "Classic tomato and mozzarella",
      "price": 12.99,
      "category": "Pizza",
      "image": "https://via.placeholder.com/150",
      "isAvailable": true
    },
    {
      "id": "item_2",
      "name": "Pepperoni Pizza",
      "description": "Pepperoni and cheese",
      "price": 14.99,
      "category": "Pizza",
      "image": "https://via.placeholder.com/150",
      "isAvailable": true
    }
  ],
  "timestamp": 1234567890
}

Error Response (Branch not found):

{
  "success": false,
  "message": "Branch not found"
}

Polling Implementation

The server automatically changes item availability every 10 seconds to simulate real-world scenarios.

Frontend polling example:

// Poll menu every 5 seconds for a specific branch
const branchId = 'branch_1';

setInterval(async () => {
  const response = await fetch(`http://localhost:3001/api/branches/${branchId}/menu`);
  const data = await response.json();
  // Update your state with new data
  // Check data.timestamp to see when data was fetched
}, 5000);

Testing with cURL

# Get all branches
curl http://localhost:3001/api/branches

# Get menu for branch_1
curl http://localhost:3001/api/branches/branch_1/menu

# Get menu for branch_2
curl http://localhost:3001/api/branches/branch_2/menu

# Try invalid branch (should return 404)
curl http://localhost:3001/api/branches/invalid_branch/menu

Notes

  • All timestamps are in milliseconds (Unix epoch)
  • Data persists in data.json across server restarts
  • CORS is enabled for all origins (adjust for production)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages