当然,您的日志会返回
undefined:您在完成请求之前先进行日志。问题不是范围,而是 异步性 。
http.request是异步的,这就是为什么它将回调作为参数的原因。做您在回调中要做的事情(传递给的
response.end):
callback = function(response) { response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(req.data); console.log(str); // your pre here if you want to use the results ! });}var req = http.request(options, callback).end();


