Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 与在本地主机(windows 7)上运行的nodejs应用程序的连接速度较慢_Node.js_Windows_Performance_Connection_Localhost - Fatal编程技术网

Node.js 与在本地主机(windows 7)上运行的nodejs应用程序的连接速度较慢

Node.js 与在本地主机(windows 7)上运行的nodejs应用程序的连接速度较慢,node.js,windows,performance,connection,localhost,Node.js,Windows,Performance,Connection,Localhost,在我的笔记本电脑上进行单元测试(mocha/supertest)时,连接到localhost(基于express/node.js的web应用程序)的速度比另一台同等机器慢 同样的硬件 Windows 7 x64 类似的网络配置(来自ipconfig,netsh interface ip show config,netsh interface ip show dns,ipconfig/displaydns。) 相同的node.js/npm配置(node.js v8.9.1和npm 5.5.1)

在我的笔记本电脑上进行单元测试(mocha/supertest)时,连接到localhost(基于express/node.js的web应用程序)的速度比另一台同等机器慢

  • 同样的硬件
  • Windows 7 x64
  • 类似的网络配置(来自
    ipconfig
    netsh interface ip show config
    netsh interface ip show dns
    ipconfig/displaydns
    。)
  • 相同的node.js/npm配置(node.js v8.9.1和npm 5.5.1)
我已经尝试了一些方法:

  • 禁用IPv6(在两台计算机上都禁用)
  • 将本地主机添加到
    hosts
    文件中
  • 刷新DNS历史记录(
    ipconfig/flushdns
  • 断开以太网电缆的连接(我也没有wifi。测试正在通过,但仍然很慢)
  • 在测试中从“localhost”更改为“127.0.0.1”
  • 删除所有我的npm依赖项并重新安装它们(在项目文件夹中,在
    C:\Users\myUser\AppData\Roaming\npm
    C:\Users\myUser\AppData\Roaming\npm缓存中,在
    C:\Users\myUser\AppData\Local\Microsoft\Typescript
    C:\Users\myUser\AppData\Local\Temp
  • 重新安装node.js/npm/vs code/typescript
  • 将npm和node.js更新至最新版本
还值得一提的是:

  • 延迟时间总是在3秒左右
  • 延迟介于我发送请求和接收请求之间(参见下面的日志)
  • 这个问题只发生在本地主机上。连接到远程API的速度与其他笔记本电脑上的速度一样快
  • 节点模块的加载时间很快
  • 无事件循环延迟(已通过检查)
  • 如果启动应用程序并尝试使用浏览器访问api,则不会延迟
  • 如果启动应用程序并使用指定的url运行测试(不使用“app”),则不会有延迟。我在应用程序中添加了以下代码:
    if(!module.parent){app.listen(3000);}
  • 如果我向localhost/127.0.0.1和端口3000发送两个测试,则第二个测试很快(参见下文)
我读过的一些相关帖子没有成功:



我创建了一个虚拟项目来重现这个问题。
app.js
文件:

'use strict';

const express = require('express');
const router = express.Router();


const app = express();

router.get('/api/v1/home', (req, res, _next) => {
  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) => {
  res.status(err.status || 500);
  console.log(err.message);
});

app.listen(3000);


module.exports = app;
'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) => {
      setTimeout(() => {
        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');
  });

});
app.test.js
文件:

'use strict';

const express = require('express');
const router = express.Router();


const app = express();

router.get('/api/v1/home', (req, res, _next) => {
  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) => {
  res.status(err.status || 500);
  console.log(err.message);
});

app.listen(3000);


module.exports = app;
'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) => {
      setTimeout(() => {
        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');
  });

});
结果如下:

> 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)
非常感谢你的帮助