在笔记本电脑上进行单元测试(mocha / supertest)期间,与本地主机(基于express / node.js的Web应用程序)的连接速度比另一台等效计算机慢。
ipconfig
,netsh interface ip show config
,netsh interface ip show dns
,ipconfig /displaydns
...的结果)我已经尝试了一些方法:
hosts
文件中ipconfig /flushdns
)C:\Users\myUser\AppData\Roaming\npm
和C:\Users\myUser\AppData\Roaming\npm-cache
中,C:\Users\myUser\AppData\Local\Microsoft\Typescript
和C:\Users\myUser\AppData\Local\Temp
还值得一提的是:
if(!module.parent) { app.listen(3000); }
我读过的一些相关文章没有成功:
Firefox and Chrome slow on localhost; known fix doesn't work on Windows 7
How to figure out why my local host site takes so long to load?
Running sites on "localhost" is extremely slow
https://technet.microsoft.com/en-us/library/bb727023.aspx
我创建了一个虚拟项目来重现该问题。
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;
app.test.js
文件:
'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)
非常感谢您的帮助