endtwist/AjaxIM — Issue #54: "Can't set headers after they are sent" in Express next() function.
Author: zkorz
Created: 2012-01-02 22:09:00+00:00
State: open

In the authentication library index.js (export.friends):

response.on('end', function() {
try {
callback(JSON.parse(data));
} catch(e) {
console.log("ERROR: "+e.message);
callback();
}
});

Whenever a user logs in the "try" callback is running and successfully logging them in, but then an error is thrown in the Express next() function located near the end of im.js:

...
res.signOff = function() { store.signOff(req.sessionID); };

next();
...

The e.message prints out "Can't set headers after they are sent".

Also, when callback() is run in the catch section with no arguments there is a fatal error because "friends" is undefined and trying to be sliced in hub.js "var friends_copy = friends.slice();".

When I comment out callback() in the catch section I get signed in with no problems. An object is returned with my username and the list of friends expected from my getuser.php and getfriends.php. I am even able to sign in other users and chat successfully, but I am getting lots of other problems if users sign in and out. I figure these other random problems could be related to the error when the user logs in.

I've been trying to get this setup for a week now and feel very close.

Thanks for your help.