0

I am trying to create a personal web proxy using javascript so that users can browse the internet through a website. Example http://webproxy.to/.

I'm trying to send the response back from a url to the user by using pipe request, but then it doesn't load the image because the html is using a relative url. How do I change the links so that it is using the absolute url of that page?

app.get('/:url', function (req, res) {
  var url = req.params.url;
  request('https://www.google.com', function (error, response, html) {
    if (!error && response.statusCode == 200) {
      req.pipe(request('https://www.google.com')).pipe(res);
    }
  });
});
jamesalone
  • 301
  • 1
  • 6
  • 19

1 Answers1

1

I think pipes are no usable in the case, you have to make modifications. Try to use something like this:

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

app.get('/:url', function (req, res) {
  var url = req.params.url;
  request(url, function (error, response, html) {    
    if (!error && response.statusCode == 200) {
      const dom = new JSDOM(html);
      const links = dom.window.document.querySelector('a');
      for(let i = 0; i < links.length; i++) {
         if (!/http/.test(links[i].href) && !/mailto/.test(links[i].href)) {
           links[i].href = `http://${url}${links[i].href}`;
         }
      }
      res.send(dom.serialize());
    }
  });
});
Lazyexpert
  • 3,106
  • 1
  • 19
  • 33