title: NodeJS express Authentication using POST returns Error Cannot Set Headers After they Are Sent
body: <p>I am trying to build a basic Auth using post</p>
  
  <pre><code> app.post("/api/auth",function(req,resp)
   {
  
     var username = req.body.username||req.param('username');
     var password = req.body.password||req.param('password');
     log("Performing Log Check");
     log(username + " "+password);
  
       var sent ={};
       sent.status =false;
       sent.authenticated = false;
  
       var query = {}
       query.sql= "SELECT * FROM voix_auth";
       query.timeout= 4000; // 40s
       connection.query(query, function (error, rows, fields)
       {
         if(!error)
         {
             var i=0;
             while(i!=rows.length)
             {
                 if(rows[i].username  == username  &amp;&amp;  rows[i].password == password)
                 {
                   log(rows);
                   sent.status = true;
                   sent.authenticated = true;
                   sent.token = tokenData;
                   log(sent);
                   break;
                 }
               i+=1;
              }
            resp.send(sent);
           }  //Error Ends
           else
           {
             log("Error Occured");
           }
  
        });  //connection Query
        log(sent);
        resp.send(sent);
    });
  </code></pre>
  
  <p>The issue here is that I get Cannot set header After They are Sent.
  So when I remove <code>resp.send()</code> this error is gone.</p>
  
  <p>But if the response I get is always false even though the user is Authenticated.</p>
  
  <p>Please help.   </p>