Why is json-server not returning individual records for my endpoint?

I am using json-server to serve data from a JSON file for my React application. When I access http://localhost:3000/delivery_centers, it correctly returns all delivery centers. However, when I try to access an individual delivery center using http://localhost:3000/delivery_centers/1, I get a “Not Found” error.

{
  "delivery_centers": [
    {
      "id": 1,
      "name": "Central Hub",
      "location": {
        "address": "123 Main St, New York City, NY, USA",
        "capacity": 1000,
        "types_of_shipments": ["express", "standard", "fragile"]
      },
      "staff": [
        { "id": 10, "name": "John Doe", "role": "Driver" },
        { "id": 11, "name": "Jane Smith", "role": "Sorter" }
      ],
      "vehicles": [
        { "id": 20, "type": "Truck", "capacity": 50 },
        { "id": 21, "type": "Van", "capacity": 20 }
      ],
      "shipments": [
        {
          "id": 123,
          "origin_center": 1,
          "destination_center": 2,
          "sender": "Acme Corporation",
          "recipient": "XYZ Inc.",
          "type": "standard",
          "weight": 30,
          "dimensions": "10x15x20",
          "value": 1000,
          "status": "in_transit",
          "route": {
            "id": 456,
            "origin": "New York City",
            "destination": "Los Angeles",
            "distance": 2400,
            "vehicle": 20
          }
        }
      ]
    },
    {
      "id": 2,
      "name": "West Coast Hub",
      "location": {
        "address": "456 West St, Los Angeles, CA, USA",
        "capacity": 800,
        "types_of_shipments": ["standard", "fragile"]
      },
      "staff": [
        { "id": 12, "name": "Mike Johnson", "role": "Driver" },
        { "id": 13, "name": "Emily Brown", "role": "Sorter" }
      ],
      "vehicles": [
        { "id": 22, "type": "Truck", "capacity": 60 },
        { "id": 23, "type": "Van", "capacity": 25 }
      ],
      "shipments": [
        {
          "id": 124,
          "origin_center": 2,
          "destination_center": 3,
          "sender": "ABC Corp.",
          "recipient": "PQR Ltd.",
          "type": "fragile",
          "weight": 20,
          "dimensions": "8x10x15",
          "value": 1500,
          "status": "delivered",
          "route": {
            "id": 457,
            "origin": "Los Angeles",
            "destination": "San Francisco",
            "distance": 380,
            "vehicle": 22
          }
        }
      ]
    }
  ]
}



Sure, here's a detailed question you can post on Stack Overflow:

Title: Why is json-server not returning individual records for my endpoint?

Body:

I am using json-server to serve data from a JSON file for my React application. When I access http://localhost:3000/delivery_centers, it correctly returns all delivery centers. However, when I try to access an individual delivery center using http://localhost:3000/delivery_centers/1, I get a "Not Found" error.

My JSON file (Logistics.json):
json
Copy code
{
  "delivery_centers": [
    {
      "id": 1,
      "name": "Central Hub",
      "location": {
        "address": "123 Main St, New York City, NY, USA",
        "capacity": 1000,
        "types_of_shipments": ["express", "standard", "fragile"]
      },
      "staff": [
        { "id": 10, "name": "John Doe", "role": "Driver" },
        { "id": 11, "name": "Jane Smith", "role": "Sorter" }
      ],
      "vehicles": [
        { "id": 20, "type": "Truck", "capacity": 50 },
        { "id": 21, "type": "Van", "capacity": 20 }
      ],
      "shipments": [
        {
          "id": 123,
          "origin_center": 1,
          "destination_center": 2,
          "sender": "Acme Corporation",
          "recipient": "XYZ Inc.",
          "type": "standard",
          "weight": 30,
          "dimensions": "10x15x20",
          "value": 1000,
          "status": "in_transit",
          "route": {
            "id": 456,
            "origin": "New York City",
            "destination": "Los Angeles",
            "distance": 2400,
            "vehicle": 20
          }
        }
      ]
    },
    {
      "id": 2,
      "name": "West Coast Hub",
      "location": {
        "address": "456 West St, Los Angeles, CA, USA",
        "capacity": 800,
        "types_of_shipments": ["standard", "fragile"]
      },
      "staff": [
        { "id": 12, "name": "Mike Johnson", "role": "Driver" },
        { "id": 13, "name": "Emily Brown", "role": "Sorter" }
      ],
      "vehicles": [
        { "id": 22, "type": "Truck", "capacity": 60 },
        { "id": 23, "type": "Van", "capacity": 25 }
      ],
      "shipments": [
        {
          "id": 124,
          "origin_center": 2,
          "destination_center": 3,
          "sender": "ABC Corp.",
          "recipient": "PQR Ltd.",
          "type": "fragile",
          "weight": 20,
          "dimensions": "8x10x15",
          "value": 1500,
          "status": "delivered",
          "route": {
            "id": 457,
            "origin": "Los Angeles",
            "destination": "San Francisco",
            "distance": 380,
            "vehicle": 22
          }
        }
      ]
    }
  ]
}
My React Component (Live.js):
javascript
Copy code
import React from "react";
import { useParams } from "react-router-dom";
import useFetch from "../Fetch/Fetch";

const Live = () => {
    const { id } = useParams();
    const { value, error, loading } = useFetch(`http://localhost:3000/delivery_centers/${id}`);

    if (loading) return <div>Loading...</div>;
    if (error) return <div>Error: {error}</div>;

    const center = value;

    return (
        <div>
            <h1>{center ? center.name : "Delivery Center Not Found"}</h1>
            {center && (
                <>
                    <p>Address: {center.location.address}</p>
                    <p>Capacity: {center.location.capacity}</p>
                    <p>Types of Shipments: {center.location.types_of_shipments.join(', ')}</p>
                    <h2>Staff</h2>
                    <ul>
                        {center.staff.map(member => (
                            <li key={member.id}>{member.name} - {member.role}</li>
                        ))}
                    </ul>
                    <h2>Vehicles</h2>
                    <ul>
                        {center.vehicles.map(vehicle => (
                            <li key={vehicle.id}>{vehicle.type} - Capacity: {vehicle.capacity}</li>
                        ))}
                    </ul>
                </>
            )}
        </div>
    );
};


Issue:
When I access http://localhost:3000/delivery_centers, it returns both delivery centers as expected:

[
  { ... },  // Details of delivery center with ID 1
  { ... }   // Details of delivery center with ID 2
]

But when I try to access an individual center like http://localhost:3000/delivery_centers/1, I get a “Not Found” error.

Question:

How can I configure json-server to correctly return individual delivery centers by their ID when accessing endpoints like http://localhost:3000/delivery_centers/1?

What I’ve Tried:

  1. Verified that the JSON server is running correctly.

  2. Ensured the id field in each object is correct.

  3. Checked the endpoint URL in the fetch request.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật