我认为处理从第三方服务器收到的响应不是一个好主意。这只会增加代理服务器的内存占用量。此外,这就是您的代码无法正常工作的原因。
而是尝试将响应传递给客户端。考虑以下代码段:
var http = require('http');http.createServer(onRequest).listen(3000);function onRequest(client_req, client_res) { console.log('serve: ' + client_req.url); var options = { hostname: 'www.google.com', port: 80, path: client_req.url, method: client_req.method, headers: client_req.headers }; var proxy = http.request(options, function (res) { client_res.writeHead(res.statusCode, res.headers) res.pipe(client_res, { end: true }); }); client_req.pipe(proxy, { end: true });}


