i try to write an module for my node.js server with require.js that just returns the object i want to get from an url. But somehow i can't return the values i get with my method. The http.get... is performed after i return the value, so that i just get "undefined" but why?
Could you please help me? Sorry if that is an stupid questen but im really new to javascript node.js and require.js.
define(['http'], function(http){
console.log('Hi ich bin pullArchiveVolume');
var response = 1;
console.log('Log 1: ' + response);
http.get("http:...", function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log("Log 2: " + response);
response = 2;
console.log("Log 3: " + response);
response = JSON.parse(body);
return response;
// console.log("Log 2 :", response);
// console.log("Got response: ", response);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
console.log("Log 4: " + response);
return response;
})
Console Output:
Hi ich bin pullArchiveVolume
Log 1: 1
log 4: 1
log 2: 1
log 3: 2
Thanks!