我正在尝试创建一个代理服务器来将HTTP GET来自客户端的请求传递到第三方网站(比如谷歌)。我的代理只需要将传入请求镜像到目标站点上的相应路径,因此如果我的客户端请求的 url 是:
127.0.0.1/images/srpr/logo11w.png
应提供以下资源:
http://www.google.com/images/srpr/logo11w.png
这是我想出的:
http.createServer(onRequest).listen(80);
function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: client_req.method
            headers: client_req.headers
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(res.statusCode, res.headers);
                 client_res.end(body);
            });
        });
        req.end();
    });
}
它适用于 html 页面,但对于其他类型的文件,它只返回一个空白页面或来自目标站点的一些错误消息(在不同站点中有所不同)。

