Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request not ending in node.js (used to work)

Tags:

node.js

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 was working just fine, till I made a couple of changes, node gives an error, and when I went back, that error wasn't there anymore, but instead of work like it was working before the end event is not being fired. So, anything inside req.addListener('end', function (){});is not called.

And even if I run another node.js that uses the same event, is not being fired either. So is like if the end event of the request is broken. But how can that be possible?

Is not the first time it happens. Last time I ended up re-installing node (after try lots of different things). I would prefer to find a solution, so I can understand the problem!

NOTE: The original code include socket.io and other kind of connections, but I've just pasted the piece of code were the app is stuck on.

It could also be useful to know how to debug the problem!

like image 521
limoragni Avatar asked Apr 09 '13 03:04

limoragni


1 Answers

@InspiredJW should get credit for pointing this out, since I had forgotten about it, but undoubtedly your problem is because of the changes in the readable streams. In order for the end event to get called you either have to attach a listener to the data event, or you have 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

like image 106
Bret Copeland Avatar answered Sep 24 '22 15:09

Bret Copeland