title: Is node terminal frozen or ddosed?
body: <p>I was working on long polling chat using node js. I might be wrong, but there was no issue in server side or client side code. I'll leave them at the end of question for those who are interested in reviewing it</p>
  <p>Main problem is that node terminal is apparently &quot;freezing&quot;.
  I load page dynamically sending a request to the server. Usually it takes half a second to load the page</p>
  <p><a href="https://i.stack.imgur.com/MhWbD.png" rel="nofollow noreferrer">page loaded in 537ms</a></p>
  <p>Then I tried running it on another browser, sent several GET and POST requests and then tried to reload the page one more time. It stucked until I focused on the node terminal window</p>
  <p><a href="https://i.stack.imgur.com/88Oyv.png" rel="nofollow noreferrer">the same page loaded in 1.5 min</a></p>
  <p>The questions are</p>
  <ul>
  <li>is it related to terminal window issues or node itself?</li>
  <li>if so, how I can prevent it?</li>
  </ul>
  <p>Browser is chrome, node v16.13.1, os Windows 10</p>
  <p>code files:</p>
  <p>main.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const http = require("http")
  const { connection, sendMessage } = require("./chatfunctions")
  const { sendFile } = require("./sendFile")
  
  const ip = "127.0.0.1"
  const port = 1337
  
  let server = http.createServer(function(req, res) {
      
      res.setHeader("Access-Control-Allow-Origin", "*")
  
      console.log("request to", req.url)
  
      switch (req.url) {
  
          case "/chat":
              res.setHeader("Content-type", "text/html")
              sendFile("./chat/index.html", res)
              break;
          
          case "/connection":
              //connection
              connection(req, res)
              break;
  
          case "/message":
              sendMessage(req, res)
              //message
              break;
          
          default:
              res.statusCode = 404
              res.end("Not found")
              //404
              break;
      }
  
  }).listen(port, ip)</code></pre>
  </div>
  </div>
  </p>
  <p>chatfunctions.js:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let clients = []
  
  
  function connection(req, res) {
  
  
      clients.push(res)
  
      console.log("new connection", clients.length)
  
  
      res.on("close", function() {
  
          clients.splice(clients.indexOf(res), 1)
  
          console.log("connection closed", clients.length)
          
      })
  
  }
  
  
  function sendMessage(req, res) {
  
      clients.forEach(function(client) {
  
          req.pipe(client)
  
  
          req.on("error", function(err) {
  
              console.error(err)
  
              client.status = 500
              client.end("Server error")
  
          })
  
          client.on("finish", function() {
              res.end("")
          })
  
      })
  
      clients = []
  
  }
  
  exports.connection  = connection;
  exports.sendMessage = sendMessage;</code></pre>
  </div>
  </div>
  </p>
  <p>sendFile.js</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const fs = require("fs");
  
  function sendFile(path, res) {
  
      let fileStream = new fs.ReadStream(path);
  
      fileStream.pipe(res);
  
  
      fileStream.on("error", function (err) {
          console.error(err);
          res.status = 500;
          res.end("Server error");
      });
  
  
      res.on("close", function () {
          fileStream.destroy();
      });
      
  }
  exports.sendFile = sendFile;</code></pre>
  </div>
  </div>
  </p>
  <p>index.html:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;style&gt;
          #form {
              padding: 10px;
          }
  
          #submit {
              width: 80px;
              height: 30px;
              border: 1px solid black;
              border-radius: 3px;
          }
  
          #message {
              width: 200px;
              height: 23px;
          }
      &lt;/style&gt;
      &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form id="form"&gt;
          &lt;input type="text" name="message" id="message"&gt;
          &lt;input type="submit" value="Submit" id="submit"&gt;
      &lt;/form&gt;
      &lt;ul id="messages"&gt;&lt;/ul&gt;
      &lt;script&gt;
          function poll() {
  
              console.log("creating connection")
  
              let connection = new XMLHttpRequest()
  
              connection.open("GET", "http://127.0.0.1:1337/connection")
  
              connection.send()
  
  
              connection.onload = function () {
  
                  let li = document.createElement("li")
  
                  li.textContent = connection.responseText
  
  
                  messages.appendChild(li)
  
                  poll()
              }
          }
  
  
          form.onsubmit = function (event) {
  
              event.preventDefault()
  
  
              let text = message.value
  
  
              let request = new XMLHttpRequest()
  
              request.open("POST", "http://127.0.0.1:1337/message")
  
              request.setRequestHeader("Content-type", "text/plain")
  
              request.send(text)
  
          }
  
          poll()
      &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>