1
的getStockValue()函数是从以下列方式另一个JavaScript文件名为:如何从node.js中的HTTP请求函数返回值?
var r=require("./stockfile");
var returedData = r.getStockValue());
这里returnedData只包含 “-START-”。
我的目标是获得从函数返回的身体对象,后接收到响应。我试过把一个return语句放入'关闭'事件处理程序中,但它不起作用。
我该怎么做?
function getStockValue() {
var http = require('http');
var options = {
host: 'in.reuters.com',
path: '/finance/stocks/overview?symbol=RIBA.BO',
method: 'GET'
};
var body = "--START--";
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data', function (chunk) {
body += chunk;
});
res.on('close', function() {
console.log("\n\nClose received!");
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
return body + '... recieved';
}
exports.getStockValue = getStockValue;
感谢会尽量回复您! :) – sbose 2012-01-04 17:51:29