◆React.js & Next.js & Node.js/Next.js(프레임워크)
[NextJs] Cors 문제 우회하기
쿠키린
2024. 10. 29. 13:49
1. Next.js의 rewrites 기능을 사용해 클라이언트가 Next.js 서버로 요청하면, 이를 백엔드 서버(여기서는 localhost:8080)로 프록시하여 전달하도록 구성
2. rewrites는 Next.js 서버를 중개 서버처럼 사용하므로, 클라이언트가 직접 localhost:8080으로 요청하는 것을 피함.
⏬next.config.js 또는 커스텀 서버 파일만들어서 해결하기
/** @type {import('next').NextConfig} */
const nextConfig = {
//output: 'standalone',
//reactStrictMode: true,
async rewrites() {
return [
{
source: '/api/:path*',
destination: `http://localhost:8080/api/:path*`,
}
]
}
};
export default nextConfig;