The request does not end with node.js (used for work)

I have this piece of code:

var app = require('http').createServer(function(req, res){ console.log(req); req.addListener('end', function () { fileServer.serve(req, res); }); }); var statics = require('node-static'); var fileServer = new statics.Server('./'); app.listen(1344, '127.0.0.1'); app.on('error', function(err){ console.log(err); }) 

It worked very well until I made a couple of changes, node gives an error, and when I returned, this error no longer existed, but instead of working as if it worked before the end event does not fire. So, nothing inside req.addListener('end', function (){}); not called.

And even if I fire another node.js that uses the same event, it also fails. Similarly, if the request termination event is interrupted. But how is this possible?

This is not the first time this has happened. Last time I finished reinstalling node (after I tried many different things). I would rather find a solution, so I can understand the problem!

NOTE. The source code includes socket.io and other types of connections, but I just pasted a piece of code, this application is stuck.

It may also be useful to learn how to debug a problem!

+6
source share
1 answer

@InspiredJW should get credit by pointing this out, since I forgot about it, but, undoubtedly, your problem is related to changes in readable streams. In order for the end event to receive a call, you need to either attach a listener to the data event, or you need to call stream.resume() .

 require('http').createServer(function(req, res){ req.addListener('end', function () { // won't ever get called in node v0.10.3 }); }); require('http').createServer(function(req, res){ req.addListener('end', function () { // will get called in node v0.10.3 because we called req.resume() }); req.resume(); }); require('http').createServer(function(req, res){ req.on('data', function (chunk) { }); req.addListener('end', function () { // also will get called because we attached a data event listener }); }); 

http://nodejs.org/api/stream.html#stream_compatibility

+13
source

All Articles