Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

how to get the complete response of a request and return to another function?

what i mean is : i want to get complete request from a treehouse api and pass it to another function by using node js , but when i tried this :

var http = require('http');
var https = require('https');
var body = '';
var request = http.createServer(function(request,response){
    response.writeHead({'content-Type':'text/plain'});
    response.write("##########################################################################");
    var req = https.get("https://teamtreehouse.com/joshtimonen.json",function(resp){
        resp.on('data',function(chunk){
            body+= chunk;
        });
        resp.on('end',function(){
            >>>>>>>>>>>>>line of problem//response.write("Name"+resp.finished+resp.statusCode+JSON.parse(body).name);
            return body;
            response.end("###########################################################################");
        });
    });
    response.end(req);

}).listen(3000);
request.on('error',function(error){
    console.log('server is getting error :: '+error.message);
});

in the line of problem marked as >>>> i am getting error because complete response not being recieved before ending write..

Jesus Mendoza
Jesus Mendoza
23,289 Points
var http = require('http');
var https = require('https');

var httpServer = http.createServer(function(serverReq, serverRes) {
    var body = '';

    var req = https.get("https://teamtreehouse.com/joshtimonen.json", function(res) {
        res.on('data', function(data) {
            body += data;
        });
        res.on('end', function() {
            serverRes.writeHead(200, {'Content-Type': 'text/plain'});
            serverRes.end(body);
        });
    });
    req.end();
});

httpServer.listen(3000, function() {
    console.log('Server is running!');
});

httpServer.on('error', function(err) {
    throw err;
});

You want to send your data when you recieved it all, so inside the res.on('end') you close the response to your server with the content you want to respond with.

Thanks Jesus but i wanted to use the response to getname, total aand other things separately like : var http = require('http'); var https = require('https'); var body = '';

var httpServer = http.createServer(function(serverReq, serverRes) {

var req = https.get("https://teamtreehouse.com/joshtimonen.json", function(res) {
    res.on('data', function(data) {
        body += data;
    });
    res.on('end', function() {
        serverRes.writeHead(200, {'Content-Type': 'text/plain'});
        serverRes.end(body);
    });
});
getName(body);
req.end();

});

httpServer.listen(3000, function() { console.log('Server is running!'); });

httpServer.on('error', function(err) { throw err; }); var getName = function(body){ console.log(body.Name+"\n"); }

but i am getting "undefined" in the console of getName method.