I have some error when using http.request for client request (node ββv0.6.18, v0.6.3), the following code is causing an error, and I have some questions.
var http = require('http'); var url = require('url'); http.createServer(function(req, res) { var data = 'ε€ζ
θͺε€η©Ίδ½ζ¨'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': 1 //ERROR }); res.end(data); }).listen(3000); function request(options, callback) { var req = http.request(options, function(res) { var data = ''; res.setEncoding = 'utf8'; res.on('data', function(chunk) { data += chunk; }); res.on('error', function(err) { callback(new Error('res error: ' + err)); }); res.on('end', function() { console.log('res on end'); callback(null, data); }); }); req.on('socket', function(socket) { socket.on('error', function(err) { console.log('socket on error'); callback('socket error: ' + err); req.abort(); }); }); req.end(); } request({ host: 'localhost', port: 3000, method: 'GET' }, function(err, data) { console.log('result, err: ' + err + ', data: ' + data); });
Outputs:
res on end result, err: null, data: socket on error result, err: socket error: Error: Parse Error, data: undefined
Here are my questions:
- Why did the res 'end' event happen before the 'error' socket event?
- If I want to cause a return error when the "Parse Error at Socket.ondata" occurs, like the code above or in any other situation, like a callback once instead of twice higher than the previous output (IF res "end" event is really hanppened before error socket events))?
I need your help! Thanks.
=================================
I found the same output codes:
res on end result, err: null, data:
in node v0.6.6 and v0.6.11. Why?