0

In local development, everything works as expected. Frontend calls the Nextjs API endpoints and receives the response with data.

However, when deployed to a IISNode, running on a Windows Server 2012 R2, API calls return only a Status Code 500 (Internal Server Error) response:

Content-Length: 21
Date: Thu, 01 Sep 2022 14:33:59 GMT
Keep-Alive: timeout=5
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET

As a side note, I've tested with the same .env from production, so the connection string for the database is correct and working.

Some alterations I've tryied where:

  1. Adding module.exports = {[webpackconfig], compress: false} to next.config.js
  2. Setting NODE_OPTIONS='--http-server-default-timeout=0'

As a example of the code, on the frontend:

// frontend.tsx
const EscreverCartinha: NextPage = () => {
  const { data, isLoading, isError } = useCallMyImplementationSWR();
  return <ReactComponents example={data} />
}

// useCallMyImplementationSWR
const fetcher = async (url: string) => {
  const res = await fetch(url);
  if (!res.ok) {
    const { msg } = await res.json();
    const error = new Error(msg);
    throw error;
  }

  return res.json();
};

const useFetcher = (url: string) => {
  let { data, error } = useSWR(url, fetcher);

  return {
    data: data,
    isLoading: !error && !data,
    isError: error,
  };
};

export function useCallMyImplementationSWR() {
  const URL = `/api/endpoint`;

  return useFetcher(URL);
}

And, on pages/api/endpoint:

export default async function handler(
  _req: NextApiRequest,
  res: NextApiResponse
) {
  await prisma.example
    .findMany()
    .then(async (exampleparam) => {
      if (example == null) throw "No results found...";
      res.status(200).json(example);
    })
    .catch(async (error: any) => {
      console.error(error);
      res.status(500).json({ msg: error });
    });
}

Final note, on possible causes, the first request to any API endpoint in local development, using both next dev and next build && next start, are slow (max. 2~3s), but subsequent request are faster.

gabril
  • 28
  • 5
  • Try using failed request tracing to see details about 500 error. – samwu Sep 02 '22 at 03:14
  • @samwu thanks a lot, with the tool I've found out that the problem was due my mistake by not running `prisma generate` before compiling the application... if you want, reply as answer, so I can check as the solution – gabril Sep 02 '22 at 14:17
  • I'm so glad that the problem has been resolved. It is so appreciated if you can mark solution as answer. – samwu Sep 05 '22 at 09:41

1 Answers1

1

Try using failed request tracing to see details about 500 error.

samwu
  • 3,857
  • 3
  • 11
  • 25