I'm attempting to use Node.js and Q.js to handle promises but appear to be 
losing the first chunk(s) of data from http requests.  My code is based on 
Q.js sample code so I can't see what I'm doing wrong.

Sample code below.  Any pointers to what I'm doing wrong?
 

> var http = require('http');
> var url = require("url");
> var Q = require ( 'q' );
> var os = require("os");
> var httpGet = function (opts) {
>     var deferred = Q.defer();
>     try {
>         http.get(opts, deferred.resolve).on("error", deferred.reject);
>     } catch (error) {deferred.reject(error);}
>     return deferred.promise;
> };
> var loadBody = function (res) {
>     var deferred = Q.defer();
>     var body = "";
>     res.on("data", function (chunk) {
>         body += chunk;
>     });
>     res.on("end", function () {
>         deferred.resolve(body);
>     });
>     res.on("close", deferred.reject);
>     return deferred.promise;
> };
> var getBody = function (uri) {
>     return httpGet(url.parse(uri)).then(function (res) {
>        if (res.statusCode.toString()[0] === "3") {
>            return httpGet(url.parse(res.headers["location"]));
>        } else {
>            return res;
>        }
>     }).then(loadBody);
> };
> http.createServer ( function ( req, res ) { 
> console.log ( "createServer response" );
> getBody ( "http://www.google.com"; ).then(function(data){ 
> console.log ( "getBody promise" ); 
> res.writeHead ( 200, {'Content-Type': 'text/plain'} );
> res.write ( data );
> res.end ( );
> });
>
> }).listen ( 8081, os.hostname() );
>
> console.log('Server running'); 

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to