Express JS proxy to call web api
Asked Answered
F

1

6

I have the following code. And a web api which returns string array,

const express = require('express');
const proxy = require('express-http-proxy');


var app = express();

app.use('/proxy', proxy('http://localhost:56660/api/values'));

app.listen(3000);

When I tried to do localhost:3000/proxy I do not get a response, But when I use app.use('/proxy', proxy('www.google.com')); , it redirects to google web site.

Please suggest me a best approach/solution: I want to create a proxy server which gets url from browser (Application), modify the url, call the new url and send the response back to browser(Application).

Floccus answered 27/2, 2018 at 20:17 Comment(0)
I
4

You can get the URL to be proxied as a query parameter, modify it and then pass that URL to proxy, like this (use instead of app.use('/proxy', proxy('http://localhost:56660/api/values'));):

app.get('/proxy', (req, res, next) => {
    const modifiedURL = modifyURL(req.query.url)
    return proxy(modifiedURL)(req, res, next)
})

You can call you server with an URL like this (GET method):

https://my.server.com/proxy?url=https://urltobeproxied.com

UPDATE:

I think this would work according to your needs:

app.use('/proxy', (req, res, next) => {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    proxy(modifiedURL)(req, res, next)
})

UPDATE2:

app.use('/proxy', proxy('http://localhost:56660/api/values', {
  proxyReqPathResolver: function(req) {
    const requestedUrl = `${req.protocol}://${req.get('Host')}${req.url}`
    const modifiedURL = modifyURL(requestedUrl)
    return require('url').parse(modifiedURL).path;
  }
}))

UPDATE3:

An example of proxy modifying the response (extracted from the package docs);

app.use('/proxy', proxy('http://localhost:56660/api/values', {
  userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
    data = JSON.parse(proxyResData.toString('utf8'));
    data.newProperty = 'exciting data';
    return JSON.stringify(data);
  }
}))
Isham answered 27/2, 2018 at 20:32 Comment(11)
Can I do similar thing without passing url as query parameter ?Floccus
How would you want to pass it to the server?Sumba
I have tried your solution by passing url as query parameter, but it still does not show any output. It says site cant be reached.Floccus
I want the proxy server to take the url and modify it by changing the domain name and add some extra query parameters and call the modified url but without passing url as query parameter .Floccus
I think I understand what you need now, see modified code above.Sumba
With the modified code, I am able to extract the url. But, It still show that site cant be reached.Floccus
after proxy(modifiedURL)(req, res, next), how do I send data back to browser or just console.log() the data received ?Floccus
There's yet another way that the express-http-proxy library offers, see updated code above.Sumba
Thanks, I was able to redirect to api call. I am wondering how to use this. Should I call the express js route and make a proxy call ? If I call expressjs will the result be returned to application ?Floccus
I want to use the data returned by the api in the application. So, now I am redirecting the request to api but can I get the data from the api to application through expressjs middleware ?Floccus
Check again. For further questions I think you should prepare a new question since the current one is drifting a bit...Sumba

© 2022 - 2024 — McMap. All rights reserved.