title: Can't set headers after they are sent on repetitive hits
body: <p>I am using node with express 4.x</p>
  
  <p>My app.js code includes</p>
  
  <pre><code>// add generic library to global space
  global._generic = generic;
  connection.connect(function(err, db) {
  if (err) {
      console.error('error connecting: ' + err.stack);
  } else {
  
      connection.query('USE hakka');
  
      var attachDB = function(req, res, next) {
          req.db = connection;
          console.log("Attached DB to req variable. Now moving for next middleware...\n");
          next();
      };
  
      var requireLogin = function(req, res, next) {
          common.checkIdandToken(req, res, next);
      }
  
      var beforeRender = function(req, res, next) {
          _generic.res = res;
          baseController.beforeRender(req, res, next);
      }
  
  
      app.post('/api/v1/user', attachDB, beforeRender, function(req, res, next) {
          user.register(req, res, next);
      });
  
      app.listen(app.get('port'), function() {
          console.log('Express server listening on port ' + app.get('port') + '...\n');
      });
  }
  });
  </code></pre>
  
  <p>And for error in my model file my code is:</p>
  
  <pre><code>if (err) {
      _generic.res.status(err.errno).send({
          success: false,
          message: err.message
      });
  }
  </code></pre>
  
  <p>It works fine most of the times. But when I make <strong>repetitive hits very fast</strong> then it gives me </p>
  
  <p>Error: Can't set headers after they are sent.</p>
  
  <p>PS:</p>
  
  <p>Register method: </p>
  
  <pre><code>register: function(req, res, next) {
  
      var self = this;
      UserModel.setDB(req.db);
  
      console.log(req.body);
      UserModel.register(req.body, function(err, result) {
          if (result.affectedRows &gt; 0) {
              res.status(200).send(self.createResponse({
                          token: token,
                          userid: sessionData.user_id
                      }, {
                          success: true,
                          message: "User has been registerd successfully"
                      }));
                      console.log("User has been registerd successfully...\n");
          } else {
              res.status(499).send(self.createResponse({}, {
                  success: false,
                  message: "User can not be registered"
              }));
              console.log("User can not be registered...\n");
          }
      });
  }
  </code></pre>
  
  <p>UserModel.register: </p>
  
  <pre><code>register: function(data, callback) {
              var self = this;
              var userid = SOMERANDOMID;
              var post = {
                  id: userid,
                  first_name: first_name,
                  last_name: last_name,
                  phone: data.phone,
                  user_status_id: 1
              };
  
              var query = self.db.query('INSERT INTO users SET ?', post,
                      function(err, result, fields) {
                          if (err) {
                              _generic.res.status(err.errno).send(
                                      self.createResponse({}, {
                                          success: false,
                                          message: err.message
                                      }));
                              console.log(err.message);
                              return false;
                          }
  
                          callback(err, result);
                      });
          }
  </code></pre>
  
  <p><strong>Phone number is UNIQUE in Database</strong></p>
  
  <p>When using same phone number the response produced is:</p>
  
  <pre><code>{
    "status": {
      "success": false,
      "message": "ER_DUP_ENTRY: Duplicate entry '9898989898' for key 'Phone'"
      }
  }
  </code></pre>
  
  <p>When I hit it again with same credentials I got the same response. But when I do repetitive hits very fast I got </p>
  
  <p>ERROR: Can't set headers after they are sent.</p>