OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Node js http-proxy request not working properly

  • Thread starter Thread starter xMayank
  • Start date Start date
X

xMayank

Guest
I am using node-http-proxy.

I am trying to proxy a request at path xxxx In my target server I am getting request. But in network tab request is hitting current host, and giving 404 Not found as route is not available in current host.

Code:
const targetMap = {
    '/xxxxxx': {
        prefix: `xxxxxxxx`
    },
};

const target = Object.keys(targetMap)
        .map((re) => {
            const match = ctx.path.match(re);
            if (match) {
                return `${targetMap[re].prefix}`;
            }
        })
        .filter((item) => item)[0];

// Proxy the request
        proxy.web(ctx.req, ctx.res, { target, changeOrigin: true }, (err) => {
            if (err) {
                LOGGER.error({ err }, 'Failed to proxy request!');
                ctx.status = 500;
                ctx.body = 'Something went wrong.';
            }
        });

<p>I am using <code>node-http-proxy</code>.</p>
<p>I am trying to proxy a request at path <code>xxxx</code> In my target server I am getting request.
But in network tab request is hitting current host, and giving 404 Not found as route is not available in current host.</p>
<pre><code>
const targetMap = {
'/xxxxxx': {
prefix: `xxxxxxxx`
},
};

const target = Object.keys(targetMap)
.map((re) => {
const match = ctx.path.match(re);
if (match) {
return `${targetMap[re].prefix}`;
}
})
.filter((item) => item)[0];

// Proxy the request
proxy.web(ctx.req, ctx.res, { target, changeOrigin: true }, (err) => {
if (err) {
LOGGER.error({ err }, 'Failed to proxy request!');
ctx.status = 500;
ctx.body = 'Something went wrong.';
}
});

</code></pre>
 

Latest posts

Top