23 lines
874 B
TypeScript
23 lines
874 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
/**
|
|
* /api/* 를 백엔드로 넘긴다. CORS를 쓰지 않는 이유가 이것이다.
|
|
*
|
|
* next.config.ts 의 rewrites 로는 안 된다 — Next가 빌드할 때 목적지를 평가해
|
|
* routes-manifest.json 에 박아버려서, 이미지를 구운 뒤에는 환경변수를 바꿔도 안 먹는다.
|
|
* proxy 는 Node 런타임에서 요청마다 돌아 그때 환경변수를 읽는다.
|
|
*
|
|
* 컨테이너에서는 서비스명(http://backend:30101), 로컬에서는 localhost.
|
|
*/
|
|
const API_ORIGIN = process.env.API_ORIGIN ?? "http://localhost:30101";
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { pathname, search } = request.nextUrl;
|
|
return NextResponse.rewrite(new URL(pathname + search, API_ORIGIN));
|
|
}
|
|
|
|
export const config = {
|
|
matcher: "/api/:path*",
|
|
};
|