// pages/api/[...path].js
import { createProxyMiddleware } from 'http-proxy-middleware';

export const config = {
  api: {
    bodyParser: false,
    externalResolver: true,
  },
};

export default function handler(req, res) {
	//console.log("handler.req: ", req);
  const proxy = createProxyMiddleware({
    target: 'http://localhost:18080', // 예: http://localhost:5000
    changeOrigin: true,
    pathRewrite: {
      '^/api': '/api',
    },
    onProxyReq: (proxyReq, req, res) => {
      console.log("🔄 [PROXYING REQUEST]");
      console.log("  → Target URL:", proxyReq.protocol + '//' + proxyReq.host + proxyReq.path);
    },

    onProxyRes: (proxyRes, req, res) => {
      console.log("✅ [RESPONSE RECEIVED]");
      console.log("  ← Status:", proxyRes.statusCode);
      console.log("  ← Headers:", proxyRes.headers);
    },
  });

  return proxy(req, res, (result) => {
	  //console.log("proxy.req: ", req);
    if (result instanceof Error) {
      throw result;
    }
  });
}
