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.

3
  • Try using failed request tracing to see details about 500 error. Commented Sep 2, 2022 at 3: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 Commented Sep 2, 2022 at 14:17
  • I'm so glad that the problem has been resolved. It is so appreciated if you can mark solution as answer. Commented Sep 5, 2022 at 9:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.