Probably a race condition between the server listening and the request trying to hit it. Try listening for a ready event from the server before making the request.
--Josh On Sat, Jul 14, 2012 at 6:54 PM, Alexey Petrushin <[email protected]> wrote: > I'm having strange node.js behavior - using `http.request` for calling > `http.server` in the same process sometimes causes error. > > This snippet of code will result in error ( https://gist.github.com/3114352 > ) > > // Server returning 'okay' to everyone. > require('http').createServer(function(req, res){ > process.nextTick(function(){ > res.end('okay') > }) > }).listen(3000) > > // Calling server. > var data = [] > var opts = {host: 'localhost', port: 3000, path: '/', method: 'get'} > var req = require('http').request(opts, function(res){ > res.on('data', function(chunk){data.push(chunk)}) > res.on('end', function(){console.log(data.join())}) > }) > req.on('error', function(err){console.log(err)}) > req.write("hello") > req.end() > > Strange thing - if You remove `nextTick` from the server or remove > `req.write` from request - the result will be ok. > > Why? Is this a bug? > > -- > 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 [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en -- Joshua Holbrook Head of Support Nodejitsu Inc. [email protected] -- 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
