0

GET /users-api Users Proxy - Original URL: /users-api Users Proxy - Forwarding to: http://localhost/ it is not even hitting 4000 if i do like target: 'http://localhost:4000/api/users', directly set target this works fine

// Create proxy middleware for users API
const usersProxy = createProxyMiddleware({
    target: 'http://localhost:4000',
    changeOrigin: true,
    pathRewrite: {
        '^/users-api': '/api/users'
    },
    on: {
        proxyReq: (proxyReq, req, res) => {
            proxyReq.setHeader('X-Proxy-Service', 'users');
            const targetUrl = `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`;
            console.log('Users Proxy - Original URL:', req.originalUrl);
            console.log('Users Proxy - Forwarding to:', targetUrl);
        },
        proxyRes: (proxyRes, req, res) => {
            proxyRes.headers['x-proxied-by'] = 'users-proxy';
        },
        error: (err, req, res) => {
            console.error('Users Proxy Error:', err);
            res.status(500).json({ 
                error: 'Users Proxy Error', 
                message: err.message 
            });
        }
    }
});
// Apply proxy middlewares to specific routes
app.use('/users-api',usersProxy);

**GET /users-api Users Proxy - Original URL: /users-api Users Proxy - Forwarding to: http://localhost/ it is not even hitting 4000 if i do like target: 'http://localhost:4000/api/users', directly set target this works fine**

1 Answer 1

0

try custom rewriting

pathRewrite: function (path, req) { return path.replace('/users-api', '/api/users') }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.