When I'm building my project with npm run build command I get a connection error because there is no API server running.
export const getStaticProps = async () => {
const res = await fetch(`127.0.0.1:3000/api/articles`);
articles = await res.json();
return {
props: {
articles
}
}
}
But it works perfectly with npm run dev. Because there is a server running at 127.0.0.1:3000
I did this workaround (use local data on build):
if (server !== 'none') {
const res = await fetch(`${server}/api/articles`);
articles = await res.json();
} else {
const articlesData = await import('../../data');
articles = articlesData.articles;
}
But it's not what I want. I want to develop both API routes and the rest of my app in one project, then run it with a simple command npm run start.
Is there a solution to this?
I've tried to run both npm run dev and npm run build on separate terminals. One time it worked, but today I can't repeat it. The build command got stuck on this output:
info - Finalizing page optimization
I think it's because those commands share the same directory - .next in the root of the project and their buildings are conflicting with each other.