📡 Learn GET API Routes in Next.js

GET route ka use hota hai server se data lane ke liye — yaani jab aap fetch karte ho.

📁 Local Static Data

Yeh data frontend ya backend dono use kar sakte hain.

export const local_employee = [
  { id: 1, name: "Zohaib", age: 23, email: "zohaibfta@gmail.com" },
  { id: 2, name: "Ali", age: 24, email: "ali@example.com" },
  { id: 3, name: "Ahtisham", age: 22, email: "ahtisham@example.com" },
];

📨 Create GET Route (All Employees)

📂 Folder: app/api/local-get-api/route.js

🔗 Test: /api/local-get-api
import { NextResponse } from "next/server";
import { local_employee } from "../../../localData/local-data";

export const GET = () => {
  return NextResponse.json(local_employee, { status: 200 });
};

🧠 Dynamic GET Route (by ID)

📂 Folder: app/api/local-get-api/[id]/route.js

import { NextResponse } from "next/server";
import { local_employee } from "../../../../localData/local-data";

export const GET = async (req, { params }) => {
    const id = parseInt(params.id); // 🟢 string ko number me convert
    const empdata = local_employee.find(emp => emp.id === id); // 🟢 id match karo

    // if (!empdata) {
    //     return NextResponse.json({ error: "Employee not found" }, { status: 404 });
    // }
    // return NextResponse.json(empdata, { status: 200 });

    let result = !empdata? ({ result :'NOT Employee found'}) : ({result : empdata})
    return NextResponse.json(result);
};