Following is the scenario:
- Host1 Nodejs has set of exposed api's which can be accessed via swagger. For instance, /api/v1
- Host2 has other set of exposed api's which can be accessed via their swagger. For instance, /xyz/api/v1
- Host2 api's can be accessed via curl too, using, curl --user "username:password" -X POST "https://host2/xyz/api/v1" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "category": "AVAILABILITY", "fromTimeStamp": "2021-10-02 13:58:00", "toTimeStamp": "2021-10-06 13:58:00"}"
- Requirement is that API "/api/v1" from Host1 swagger will hit Host1 nodejs, from there it has to be forwarded to Host2 API "/xyz/api/v1" with user credentials.
I am using http-proxy to proxy the request from host1 to host2, but I am not able to send user credentials along with that. Any idea how to do that?
I am using below code.
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
proxy.on('proxyReq', function (proxyReq: any, req: any) {
});
proxy.on('proxyRes', function (proxyRes: any, req: any, res: any) {
res.setHeader('Access-Control-Allow-Origin', '*');
});
proxy.on('error', function (err: any, req: any, res: any) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end("An error occured");
});
this.express.use("/", function (req, res, next) {
if(req.originalUrl.includes("/api/v1"))
{
proxy.web(req, res, {
target: "https://host2/xyz/", // link to target service
secure: false
});
}
else
next()
});