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:
module.exports = {[webpackconfig], compress: false}
to next.config.js
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.
Try using failed request tracing to see details about 500 error.