title: NodeJS server timeout
body: <p>I have an Express NodeJS instance installed on my server. API stop responding after when "I guess" is overloaded. Response is below.</p>
  
  <pre><code>POST /my/api/result - - ms - -
  </code></pre>
  
  <p>I tried following in <code>./bin/www</code></p>
  
  <pre><code>server.on('connection', function(socket) {
    console.log("A new connection was made by a client.");
    socket.setTimeout(300 * 1000);
  });
  </code></pre>
  
  <p>and <code>/app.js</code></p>
  
  <pre><code>var timeout = require('connect-timeout');
  app.use(timeout(6000000));
  app.use(haltOnTimedout);
  function haltOnTimedout(req, res, next){
      if (!req.timedout) next();
  }
  </code></pre>
  
  <p>but this din't fix the issue.</p>
  
  <p>API definition</p>
  
  <pre><code>router.post('/search',function (req,res,next) {
  if(typeof req.body.q == '' || typeof req.body.q == 'undefined'){
      return res.format({
          json: function () {
              res.send({
                  status: 404,
                  message: "Mandatory parameter/header is missing."
              });
          }
      });
  }else{
      mySQLConn.pool(function (err, conn) {
          if(err){
              return res.format({
                  json: function () {
                      res.send({
                          status: 500,
                          message: "Unable to connect to database. " + err
                      })
                  }
              });
          }else{
              var q = req.body.q;
              var returnData = [];
              if(q.length&gt; 0){
  
                  var query = "select * from abc where title like '" + q + "%' limit 10"
                  conn.query(query, function (err, result) {
                      if(err){
                          return res.format({
                              json: function () {
                                  res.send({
                                      status: 500,
                                      message: "error while fetching data "+ err
                                  })
                              }
                          });
                      }else{
  
                          return res.format({
                              json: function () {
                                  res.send({
                                      status: 200,
                                      data: result
                                  })
                              }
                          });
                      }
                  })
              }else{
                  return res.format({
                      json: function () {
                          res.send({
                              status: 200,
                              data: returnData
                          })
                      }
                  });
              }
          }
      });
  }
  
  });
  </code></pre>
  
  <p>once the response is halted <code>top</code> on linux server has following response.</p>
  
  <pre><code> PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                         
  16042 nod       20   0  157700   2144   1520 R  0.3  0.2   0:00.36 top                             
  24692 mysql     20   0 1830988 374416   4800 S  0.3 36.8   5:39.80 mysqld 
  </code></pre>
  
  <p><code>nod</code> is the user from which the node application is running.</p>