Internal Server Error ECONNREFUSED nextjs 13 app directory (localhost:3000)

im only pulling data from sanity via a api endpoint, but no matter what i try, it keeps randomly crashing. i tried different versions of node, same issue, different versions of nextjs also same issue.

//CONSOLE ERROR
       TypeError: fetch failed
            at Object.fetch (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/compiled/undici/index.js:1:26669)
            at runMicrotasks (<anonymous>)
            at processTicksAndRejections (node:internal/process/task_queues:96:5)
            at async invokeRequest (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:17:12)
            at async invokeRender (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/server/lib/router-server.js:254:29)
            at async handleRequest (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/server/lib/router-server.js:447:24)
            at async requestHandler (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/server/lib/router-server.js:464:13)
            at async Server.<anonymous> (/Users/user/Desktop/Builds/social-media-next-august-2023/node_modules/next/dist/server/lib/start-server.js:117:13) {
          cause: Error: connect ECONNREFUSED 127.0.0.1:55280
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
            errno: -61,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '127.0.0.1',
            port: 55280
          }
        }

//file structure
    app/api/post/route.tsx
    app/(user)/page.tsx
    app/(user)/layout.tsx
    app/(admin)/studio/[[index]]/studio.tsx




//PACKAGE.JSON
      {
            "name": "social-media-next-august-2023",
            "version": "0.1.0",
            "private": true,
            "scripts": {
                "dev": "next dev",
                "build": "next build",
                "start": "next start",
                "lint": "next lint"
            },
            "dependencies": {
                "@react-oauth/google": "^0.11.1",
                "@sanity/client": "^6.4.9",
                // ... other dependencies ...
                "zustand": "^4.4.1"
            },
            "devDependencies": {
                "eslint-plugin-tailwindcss": "^3.13.0"
            }
        }





//route.ts
     import { allPostsQuery } from "@src/lib/queries";
        import { client } from "@src/safe/sanity/lib/client";
        import { NextResponse } from "next/server";
    
        export const GET = async () => {
            try {
                const query = allPostsQuery();
                const data = await client.fetch(query);
                return NextResponse.json(data, { status: 200 });
            } catch (error) {
                console.error("Error fetching data:", error);
                return NextResponse.json({ response: "Error" }, { status: 400 });
            }
        };





//page.tsx
     export default async function Home() {
            const BASE_URL = process.env.NEXT_PUBLIC_API_URL;
            const fetchPosts = await fetch(`${BASE_URL}/api/post`, {
                next: {
                    revalidate: 3600,
                },
            });
            const allPosts: Post[] = await fetchPosts.json();
    
            return (
                <main className="w-full">
                    <Content posts={allPosts} />
                </main>
            );
        }

I am trying to pull data from Sanity on my Next.js app, but the server keeps crashing due to a “500 Internal Server Error.”

Leave a Comment