req.on error should be assigned before calling req.write and req.end or you will run in variety of race conditions.