title: Slow connection to a nodejs application running on localhost (windows 7)
body: <p>Connection to localhost (web app based on express/node.js) during unit testing (mocha/supertest) on my laptop is slow compared to another equivalent machine.</p>
  
  <ul>
  <li>Same hardware</li>
  <li>Windows 7 x64</li>
  <li>Similar network configuration (results from <code>ipconfig</code>, <code>netsh interface ip show config</code>, <code>netsh interface ip show dns</code>, <code>ipconfig /displaydns</code>...)</li>
  <li>Same node.js/npm configuration (node.js v8.9.1 and npm 5.5.1)</li>
  </ul>
  
  <p>I have already tried a few things:  </p>
  
  <ul>
  <li>Disable IPv6 (it is disable on both machines)</li>
  <li>Add localhost into the <code>hosts</code> file</li>
  <li>Flush DNS history (<code>ipconfig /flushdns</code>)</li>
  <li>Disconnecting my ethernet cable (I have no wifi either. Tests are passing but are still slow)</li>
  <li>Change from 'localhost' to '127.0.0.1' in the test</li>
  <li>Delete all my npm dependencies and re-install them (in project folder, in <code>C:\Users\myUser\AppData\Roaming\npm</code> and <code>C:\Users\myUser\AppData\Roaming\npm-cache</code>, in <code>C:\Users\myUser\AppData\Local\Microsoft\Typescript</code> and <code>C:\Users\myUser\AppData\Local\Temp</code></li>
  <li>Reinstall node.js/npm/vs code/typescript</li>
  <li>Update npm and node.js to the latest version</li>
  </ul>
  
  <p>It is also worth mentioning that:</p>
  
  <ul>
  <li>the delay is always about 3s</li>
  <li>the delay is between the time I send the request and the time I receive it (<em>cf.</em> logs below)</li>
  <li>the problem only happens with localhost. Connection to remote APIs is as fast as on the other laptop</li>
  <li>the loading time of the node-modules is fast</li>
  <li>no event loop latency (checked with <a href="https://www.npmjs.com/package/event-loop-lag" rel="nofollow noreferrer">event-loop-lag</a>)</li>
  <li>there is no delay if I start the application and try to access the api with a browser</li>
  <li>there is no delay if I start the application and run the test with url specified (not using 'app'). I added the following code in the app: <code>if(!module.parent) { app.listen(3000); }</code></li>
  <li>if I send 2 tests to localhost/127.0.0.1 and port 3000, the 2nd test is fast (<em>cf.</em> below)</li>
  </ul>
  
  <p>Some related posts I have read without success:<br>
  <a href="https://stackoverflow.com/questions/1726585/firefox-and-chrome-slow-on-localhost-known-fix-doesnt-work-on-windows-7">Firefox and Chrome slow on localhost; known fix doesn&#39;t work on Windows 7</a><br>
  <a href="https://stackoverflow.com/questions/6405121/how-to-figure-out-why-my-local-host-site-takes-so-long-to-load?noredirect=1&amp;lq=1">How to figure out why my local host site takes so long to load?</a><br>
  <a href="https://stackoverflow.com/questions/2386299/running-sites-on-localhost-is-extremely-slow?noredirect=1&amp;lq=1">Running sites on &quot;localhost&quot; is extremely slow</a><br>
  <a href="https://technet.microsoft.com/en-us/library/bb727023.aspx" rel="nofollow noreferrer">https://technet.microsoft.com/en-us/library/bb727023.aspx</a></p>
  
  <p>I have created a dummy project to reproduce the problem.<br>
  <code>app.js</code> file:</p>
  
  <pre><code>'use strict';
  
  const express = require('express');
  const router = express.Router();
  
  
  const app = express();
  
  router.get('/api/v1/home', (req, res, _next) =&gt; {
    console.log('home api - request received - ', new Date().toUTCString());
    res.sendStatus(200);
  });
  app.use(router);
  
  app.use(function (req, res) {
    res.status(404).send();
  });
  
  app.use((err, req, res, next) =&gt; {
    res.status(err.status || 500);
    console.log(err.message);
  });
  
  app.listen(3000);
  
  
  module.exports = app;
  </code></pre>
  
  <p><code>app.test.js</code> file:</p>
  
  <pre><code>'use strict';
  
  const assert = require('assert');
  const rp = require('request-promise');
  const app = require('./app');
  const supertest = require('supertest');
  
  
  describe('', function () {
  
    it('1st mocha test run fast', async function () {
      const res = await new Promise((resolve, _reject) =&gt; {
        setTimeout(() =&gt; {
          resolve('ok');
        }, 100);
      });
      assert.equal(res, 'ok');
    });
  
    it('calling app api with supertest', async function () {
      console.log('home api supertest - send request - ', new Date().toUTCString());
      await supertest(app).get('/api/v1/home').expect(200);
    });
  
    it('calling app api with request-promise (127.0.0.1)', async function () {
      console.log('home api request-promise - send request - ', new Date().toUTCString());
      const res = await rp('http://127.0.0.1:3000/api/v1/home')
        .then(function (_htmlString) {
            return 'ok'; 
        })
        .catch(function (err) {
            console.log(err);
            return 'nok';
        });
      assert.equal(res, 'ok');
    });
  
    it('calling app api with request-promise (localhost)', async function () {
      console.log('home api request-promise - send request - ', new Date().toUTCString());
      const res = await rp('http://localhost:3000/api/v1/home')
        .then(function (_htmlString) {
            return 'ok'; 
        })
        .catch(function (err) {
            console.log(err);
            return 'nok';
        });
      assert.equal(res, 'ok');
    });
  
  });
  </code></pre>
  
  <p>The result is as follows:</p>
  
  <pre><code>&gt; mocha ./api.test.js --exit --timeout 5000
  
      √ 1st mocha test run fast (101ms)
  home api supertest - send request -  Mon, 06 Aug 2018 02:47:23 GMT
  home api - request received -  Mon, 06 Aug 2018 02:47:26 GMT
      √ calling app api with supertest (3020ms)
  home api request-promise - send request -  Mon, 06 Aug 2018 02:47:26 GMT
  home api - request received -  Mon, 06 Aug 2018 02:47:29 GMT
      √ calling app api with request-promise (127.0.0.1) (3011ms)
  home api request-promise - send request -  Mon, 06 Aug 2018 02:47:29 GMT
  home api - request received -  Mon, 06 Aug 2018 02:47:29 GMT
      √ calling app api with request-promise (localhost)
  
    4 passing (6s)
  </code></pre>
  
  <p>Thank you very much for your help</p>