Node.js: How do Proxy sites deal with relative Urls?
I've created a relatively simple proxy开发者_C百科 in Node, which allows me to download pages and display them. This is fine, although some scripts, links, forms and images seem to be broken since they are pointing to relative files. As a project I'm trying to create a fully functional web proxy.
How do sites like Proxify solve this problem?
Program for reference:
var app = require('express').createServer();
var request = require('request'),
sys = require('sys'),
fs=require('fs');
app.get('/url', function(req, res){
console.log(req.query.link);
request({ uri: req.query.link,
headers: {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0"}
}, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error when contacting google.com')
}
res.send(body, {"Content-type": "text/html"});
res.end();
});
});
Right now your code is only proxying the html file, and the client is grabbing the rest directly from the real site. You'll want to use something like node jQuery to replace all src/href in the document to make them go through your proxy, and at the same time you can check if they're relative or not and if they are prepend the current url and then create your proxy url.
精彩评论