title: How to close the ports opened through "http-server"programatically?
body: <p>I am trying to host HTML pages using http-server. Once I run the command to host the pages in consecutive http-servers, stop the command and run it again, I get the following Error:</p>
  
  <pre><code>Error: listen EADDRINUSE: address already in use 0.0.0.0:5501
      at Server.setupListenHandle [as _listen2] (net.js:1290:14)
      at listenInCluster (net.js:1338:12)
      at doListen (net.js:1469:7)
      at internalTickCallback (internal/process/next_tick.js:72:19)
      at process._tickCallback (internal/process/next_tick.js:47:5)
      at Function.Module.runMain (internal/modules/cjs/loader.js:763:11)
      at startup (internal/bootstrap/node.js:303:19)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
  Emitted 'error' event at:
      at emitErrorNT (net.js:1317:8)
      at internalTickCallback (internal/process/next_tick.js:72:19)
      [... lines matching original stack trace ...]
      at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
  </code></pre>
  
  <p>I tried searching ways to close these ports through program. But I could not find any suitable solution</p>
  
  <p>The below code is trying host every folder present inside the hosted folder in consecutive ports hosted by http-server.</p>
  
  <p>runScripts.js file:</p>
  
  <pre><code>function runServer() {
    hostedFolder = '../hosted';
    nfiles = []
    fs.readdirSync(hostedFolder).forEach(file =&gt; {
      nfiles.push(file);
    })
    for (j = 0; j &lt; nfiles.length; j++) {
      server = "http-server ../hosted/" + nfiles[j] + "  -p 550" + j
      console.log("Hosted server",server)
      runMultipleCommandWithWait2(server)
    }
  }
  
  function runMultipleCommandWithWait2(server) {
    console.log("Starting the server now...")
  
    Promise.coroutine(function* () {
      var commands = [server];
      for (var i = 0; i &lt; commands.length; i++) {
        yield cmd.run(commands[i]);
      }
    })();
  }
  </code></pre>
  
  <p>I want a way to kill these ports programatically so that I can continuously and consecutively host the folders without any error.</p>