httprequest - Node.js` "http.get" doesn't return full response body to a browser but just a piece of <head> tag. How to get full response? -
i experience strange behavior node.js http.get. make request url ajax , want result browser. piece of <head> tag content , nothing more, no <body> content. if send result system console (console.log(chunk)) result want - full page. here steps:
// simple jquery ajax $.ajax({ type: "get", url: "/myapppath", // send app's url data: {foo: "bar"}, success: onload, // callback, see bellow error: onerror, datatype: "text" }); // callback function want insert result <div id="baz"> function onload(resp) { document.getelementbyid("baz").innnerhtml = resp; } // in /myapppath http.get("http://stackoverflow.com/", function(result) { result.setencoding('utf8'); result.on("data", function(chunk) { console.log(chunk); // returns whole page system console res.end(chunk); // returns piece of <head> tag browser. }); }); so in <div id="baz"> piece of <head> tag of http://stackoverflow.com/ request, no <body> tag , content. that's in <div id="baz"> instead of whole page:
<!doctype html> <html> <head> <title>stack overflow</title> <link rel="shortcut icon" href="https://cdn.sstatic.net/stackoverflow/img/favicon.ico"> <link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png"> <link rel="search" type="application/opensearchdescription+xml" title="stack overflow" href="/opensearch.xml"> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.sstatic.net/js/stub.js?v=dd6898efd655"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/stackoverflow/all.css?v=8a5907e853ab"> <link rel="alternate" type="application/atom+xml" title="feed of recent questions" href="/feeds"> <script type="text/javascript" defer> </script> <script type="text/javascript"> stackexchange.init({"stackauthurl":"https://stackauth.com","servertime":1374771485,"stylecode":true,"enableuserhovercards":true,"site":{"name":"stack overflow","description":"q&a professional , enthusiast programmers","isnoticestabenabled":true,"recaptchapublickey":"6ldchgiaaaaaajwgpizrqsofao0pu6s44xt8atwc","enablesocialmediainsharepopup":true},"user":{"fkey":"b1d105a0cf61e49216c5750a6ad60dec","isanonymous":true}}); stackexchange.using.setcachebreakers({"js/prettify-full.js":"6c261bebf56a","js/moderator.js":"7cf00e91ce39","js/full-anon.js":"c5bf51314708","js/full.js":"02e9182c23d3","js/wmd.js":"2f79c03846d5","js/third-party/jquery.autocomplete.min.js":"e5f01e97f7c3","js/mobile.js":"e8e23ad37820","js/help.js":"6e6623243cf6","js/tageditor.js":"450c9e8426fc","js/tageditornew.js":"b6c68ad4c7dd","js/inline-tag-editing.js":"8e84e8a137f7","js/revisions.js":"7273bb714bba","js/review.js":"2b3ae123e376","js/tagsuggestions.js":"aa48ef6154df","js/post-validation.js":"bb996020492a","js/explore-qlist.js":"1c5bbd79b562","js/events.js":"37756ef3ba47"}); </script> <script type="text/javascript"> stackexchange.using("gps", function() { stackexchange.gps.init(true); }); </script> <script type="text/javascript"> stackexchange.ready(function () { $('#nav-tour').click(function () { stackexchange.using("gps", function() { stackexchange.gps.track("aboutpage.click", { aboutclick_location: "headermain" }, true); }); }); }); </script> </h but in console.log(chunk) whole page printed in console said above.
what going on? why http.get doesn't return full response browser? did miss? cuts response?
console.log(chunk); doesn't log entire page @ once, keep logging each chunk more 'data' arrives.
res.end(), on other hand, becomes no-op after 1st call closes connection, 1st chunk included.
what can res.write() each chunk of 'data', waiting 'end' res.end():
http.get("http://stackoverflow.com/", function (result) { result.on('data', function (chunk) { res.write(chunk); }); result.on('end', function () { res.end(); }); }); or, since result stream.readable (incomingmessage) , res presumably stream.writable (guessing serverresponse), should able .pipe() them:
http.get('http://stackoverflow.com', function (result) { result.pipe(res); });
Comments
Post a Comment