因爲你正在返回resp
它看起來像你忘記了請求是異步的。請嘗試以下操作:
var rest = require('restler');
var getResp = function(url){
rest.get(url).on('complete', function(response){
console.log(response);
});
};
getResp('http://google.com/');
// output:
// <!doctype html><html itemscope="itemscope" ....
由於其異步性質,最好將值傳遞給接收回調。以這個小例子爲例:
var rest = require('restler');
// process an array of URLs
['http://google.com/',
'http://facebook.com/'].forEach(function(item) {
getResp(item);
});
function getResp(url){
rest.get(url).on('complete', function(response){
processResponse(response);
});
};
// data will be a Buffer
function processResponse(data) {
// converting Buffers to strings is expensive, so I prefer
// to do it explicitely when required
var str = data.toString();
}
數據一旦進入,您就可以像其他變量一樣傳遞它。
你試過'getResp('http://google.com')'? –
東西..我得到'''' –