title: Node JS running but not responding. Works but then periodically stops responding
body: <p>I've created my first ever NodeJS Api (first time JS too) and so am coming up against a few issues and was really hoping for some help/direction.</p>
  <p>My current issue is that the API works however every now and then (more than once a day) it stops responding. (testing using postman). It doesnt respond with 'no response' it just keeps trying as if waiting for a response.</p>
  <p>When I log into the node i use:</p>
  <pre><code>lsof -i tcp:3000
  COMMAND   PID  USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
  node    22361 [myserver] 18u  IPv6 190588033      0t0  TCP *:hbci (LISTEN)
  kill -9 22361
  </code></pre>
  <p>When I kill it, i wait 10 seconds and it starts up again because i have installed FOREVER.</p>
  <p>The issue is that it seems the node is there just... not functioning. If it crashed, wouldn't it restart and be working again but instead it is just &quot;there&quot;.</p>
  <p>How can I diagnose this?</p>
  <p>I do also have Nodemon installed but can't really get it working properly as I get an EAINUSE error</p>
  <ul>
  <li><p>I have created the file on my VPS.</p>
  </li>
  <li><p>SSH using Visual Stdio Code on my Win10 PC.</p>
  </li>
  <li><p>Watched a few youtube vids to get me running</p>
  </li>
  <li><p>My main js file is very basic:</p>
  </li>
  </ul>
  <p>js file:</p>
  <pre><code>const app = require('./app');
  const port = process.env.port || 3000;
  app.listen(port);
  </code></pre>
  <p>At the moment I can't seem to find &quot;why&quot; the node goes from working and responsive to stagnant, running but not actually working!</p>
  <p>Happy to share code, just have about 12 js files and didn't want to throw too much on here.</p>
  <p>Package.json:</p>
  <pre><code>{
    &quot;name&quot;: &quot;qudaapi&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;NodeJSAPI&quot;,
    &quot;main&quot;: &quot;qudaserver.js&quot;,
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;node qudaserver.js&quot;
        },
    &quot;author&quot;: &quot;GAngel&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;dependencies&quot;: {   
      &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
      &quot;dotenv&quot;: &quot;^8.2.0&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;jsonwebtoken&quot;: &quot;^8.5.1&quot;,
      &quot;morgan&quot;: &quot;^1.10.0&quot;,
      &quot;mysql&quot;: &quot;^2.18.1&quot;
    }
  }
  </code></pre>
  <p>App.js</p>
  <pre><code>const express = require('express');
  const morgan = require('morgan');
  const bodyParser = require('body-parser');
  
  const app = express();
  
  app.use(morgan('dev'));
  app.use(bodyParser.urlencoded({extended: false}));
  app.use(bodyParser.json());
  
  app.use((req,res,next)=&gt;{
      res.header(&quot;Access-Control-Allow-Origin&quot;,&quot;*&quot;);
      res.header(&quot;Access-Control-Allow-Headers&quot;,&quot;Origin,X-Requested-With,Content-Type,Accept,Authorization&quot;);
  
      if (req.method === 'OPTIONS'){
          res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');
          return res.status(200).json({});
      }
      next();
  });
  
  //Import Routes
  const chemistRoutes = require('./api/routes/chemists');
  const smsHxRoutes = require('./api/routes/smsHx');
  const authRoute = require('./api/routes/auth');
  const webhookRoutes = require('./api/routes/stripehook');
  const orderRoutes = require('./api/routes/orders');
  const comboDataRoutes = require('./api/routes/comboData');
  const staffRoutes = require('./api/routes/staff');
  const orderListsRoutes = require('./api/routes/orderLists');
  const contactLogRoutes = require('./api/routes/contactLog');
  const licenseRoutes = require('./api/routes/license');
  
  //Route Middleware
  app.use('/smsHx',smsHxRoutes);
  app.use('/chemists',chemistRoutes);
  app.use('/register',authRoute);
  app.use('/stripehook',webhookRoutes);
  app.use('/orders',orderRoutes);
  app.use('/comboData',comboDataRoutes);
  app.use('/staff',staffRoutes);
  app.use('/orderLists',orderListsRoutes);
  app.use('/contactLog',contactLogRoutes);
  app.use('/license',licenseRoutes);
  
  
  
  app.use((req,res,next) =&gt; {
      const error = new Error('Endpoint not Found');
      error.status = 404;
      next(error);
  })
  
  app.use((error,req,res,next) =&gt; {
  res.status(error.status || 500);
  res.json({
      error: {
          message: error.message
      }
      });
  
  });
  
  
  module.exports = app;
  </code></pre>
  <p>Section that is causing some kind of failed loop:</p>
  <pre><code>//Login_Get APIkey
  router.post('/login',verifyQUDA,async (req,res) =&gt; {
      let loginChemist = req.body;
      const realPass = loginChemist.chemistPassword;
      
      // CHECK Password  
     
      var sqlString = &quot;SELECT * From tblChemists WHERE userName = ?;&quot;;
  
          connection.query(sqlString,[loginChemist.userName], async (err,rows,fields)=&gt;{
              if (rows &amp;&amp; Array.isArray(rows) &amp;&amp; rows.length) {
  
                      const savedHash = rows[0].chemistpass;
                      const chemistID = rows[0].chemistID;
                      const validPass = await bcrypt.compare(realPass,savedHash);
                      
  
                      if(!validPass){
                          return res.status(200).json({
                              Result: false
                                  })
                      }else{
  
                          const token = jwt.sign({_id: chemistID},process.env.TOKEN_SECRET);
                          res.header('auth-token',token);
                          return res.status(200).json({
                              Result: true,
                              API_Token: token
                                  })
                      } 
  
              }       
              })
          
          
          
  
  })
  </code></pre>
  <p>So my code runs and I can use the API, I seem to be able to use it fine (all routes) and get the expected responses, however if I log in randomly and just do a test, sometimes it is down and POSTMAN just keeps spinning waiting for a response but when I check... it is still 'running'</p>
  <p>Really keen to know what I should do to diagnose?</p>
  <p>EDIT:
  The code above is the section which sometimes loops or causes issues.
  I have found that the api is in fact running as other API calls are working AND this API call &quot;works&quot; if I do not add the header.
  Therefore I can get responses 500 if no header is there, or if the header API Key is incorrect.</p>
  <p>The issue seems to be if the header is correct, and user and pass are correct sometimes I just get a looped response...
  The code DOES work however and give the API_Token I need here and there but sometimes just loops like a nutcase!</p>