Nikola Notevski
Nikola Notevski

Reputation: 21

http-proxy-middleware in nodejs not redirect to specific microservice

Hi I have problem with http-proxy-middleware in nodejs Api Gateway.

const proxyOptions: Options = {
    target: 'http://xxxx-xxx-xxxxx:3010/auth',
    changeOrigin: true,
    secure: false,
    // pathRewrite: (path) => path.replace(prefix, ""), // Strip the prefix before forwarding
    pathRewrite: { '^/api': '' }, // Removes /api/auth and sends the remaining part to /auth in mcd-auth-service
    logger: console,
    on: {
        proxyReq: (proxyReq, req) => {

            const expressReq = req as Request;
            console.log(`Proxying request to: ${proxyReq.path}`);
            console.log(`Proxy Request Method: ${req.method}`);
            console.log(`Original Request Path: ${expressReq.originalUrl}`);
            // console.log(`Request Headers: ${JSON.stringify(req.headers)}`);
            // console.log(`Request Body: ${JSON.stringify(expressReq.body)}`);
        },
        proxyRes: (proxyRes, req, res) => {
            const expressReq = req as Request;
            console.log(`Received response with status ${proxyRes.statusCode} from ${expressReq.originalUrl}`);
        },
        error: (err, req, res) => {
            console.error(`Proxy error: ${err.message}`);
            res.end("Service Unavailable");
        },
    }
}
// Proxy middleware for handling service redirection
// Object.keys(serviceMap).forEach((prefix) => {
app.use(
    // prefix,
    "/api/auth",
    createProxyMiddleware(proxyOptions)
);

I have 3 docker images for frontend, API gateway, and auth-service. Frontend and API gateway are in the same repository but the auth-service is in another repository. The proxy redirects when I run curl command from terminal but when I try to run with a regular flow from front end it's not working, request is pending. From frontend to API gateway requests is coming but from API to auth-service not redirect. Any help I appreciate

Upvotes: 0

Views: 30

Answers (0)

Related Questions