title: Error: Can't set headers after they are sent to the client
body: <p>I'm fairly new to Node.js and I am having some issues.</p>
  
  <p>I am using Node.js 4.10 and Express 2.4.3.</p>
  
  <p>When I try to access <a href="http://127.0.0.1:8888/auth/facebook" rel="noreferrer">http://127.0.0.1:8888/auth/facebook</a>, i'll be redirected to <a href="http://127.0.0.1:8888/auth/facebook_callback" rel="noreferrer">http://127.0.0.1:8888/auth/facebook_callback</a>.</p>
  
  <p>I then received the following error:</p>
  
  <pre><code>Error: Can't render headers after they are sent to the client.
      at ServerResponse.&lt;anonymous&gt; (http.js:573:11)
      at ServerResponse._renderHeaders (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:64:25)
      at ServerResponse.writeHead (http.js:813:20)
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/auth.strategies/facebook.js:28:15
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:113:13
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/strategyExecutor.js:45:39)
      at [object Object].pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:32:3)
      at [object Object].halt (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:29:8)
      at [object Object].redirect (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:16:8)
      at [object Object].&lt;anonymous&gt; (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/auth.strategies/facebook.js:77:15)
  Error: Can't set headers after they are sent.
      at ServerResponse.&lt;anonymous&gt; (http.js:527:11)
      at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:195:11)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
      at param (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:189:13)
      at pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:191:10)
      at Object.router [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:197:6)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
      at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
  Error: Can't set headers after they are sent.
      at ServerResponse.&lt;anonymous&gt; (http.js:527:11)
      at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
      at param (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:189:13)
      at pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:191:10)
      at Object.router [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:197:6)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
      at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
  Error: Can't set headers after they are sent.
      at ServerResponse.&lt;anonymous&gt; (http.js:527:11)
      at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
      at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
      at HTTPServer.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:211:3)
      at Object.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:105:14)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
  Error: Can't set headers after they are sent.
      at ServerResponse.&lt;anonymous&gt; (http.js:527:11)
      at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
      at HTTPServer.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:211:3)
      at Object.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:105:14)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:323:9
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:338:9
  
  node.js:134
          throw e; // process.nextTick error, or 'error' event on first tick
          ^
  Error: Can't set headers after they are sent.
      at ServerResponse.&lt;anonymous&gt; (http.js:527:11)
      at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
      at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:323:9
      at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:338:9
      at Array.&lt;anonymous&gt; (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session/memory.js:57:7)
      at EventEmitter._tickCallback (node.js:126:26)
  </code></pre>
  
  <p>The following is my code:</p>
  
  <pre><code>var fbId= "XXX";
  var fbSecret= "XXXXXX";
  var fbCallbackAddress= "http://127.0.0.1:8888/auth/facebook_callback"
  
  var cookieSecret = "node";     // enter a random hash for security
  
  var express= require('express');
  var auth = require('connect-auth')
  var app = express.createServer();
  
  
  app.configure(function(){
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(express.cookieParser());
      app.use(express.session({secret: cookieSecret}));
      app.use(auth([
          auth.Facebook({
              appId : fbId,
              appSecret: fbSecret,
              callback: fbCallbackAddress,
              scope: 'offline_access,email,user_about_me,user_activities,manage_pages,publish_stream',
              failedUri: '/noauth'
          })
      ]));
      app.use(app.router);
  });
  
  
  app.get('/auth/facebook', function(req, res) {
    req.authenticate("facebook", function(error, authenticated) {
      if (authenticated) {
        res.redirect("/great");
        console.log("ok cool.");
        console.log(res['req']['session']);
      }
    });
  });
  
  app.get('/noauth', function(req, res) {
    console.log('Authentication Failed');
    res.send('Authentication Failed');
  });
  
  app.get('/great', function( req, res) {
    res.send('Supercoolstuff');
  });
  
  app.listen(8888);
  </code></pre>
  
  <p>May I know what is wrong with my code?</p>