function(req, res, next)
res.charset = 'utf-8'
res.cookie(name, val[, options])
res.json(obj[, headers|status[, status]])
res.render(view[, options[, fn]])
res.send(body|status[, headers|status[, status]])
res.sendfile(path[, options[, callback]])
res.statusCode = 404
res.writeHead(statusCode, [reasonPhrase], [headers])
In your case, you called res.redirect(), which caused the response to become Finished. Then your code threw an error (res.req is null). and since the error happened within your actual function(req, res, next) (not within a callback), Connect was able to catch it and then tried to send a 500 error page. But since the headers were already sent, Node.js's setHeader threw the error that you saw.
Response can be in either Head/Body and remains in its current state:
Response must be in Head and becomes Finished:
Response must be in Head and remains in Head:
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written. For example, look for callbacks that are accidentally called twice, or any error that happens after the body is sent.
also watch out for this classic mistake: res.redirect() doesn't stop statement execution... so return after it. Otherwise other code could be executed which could unintentiallly cause the famous header error. Thanx for the explanation!