javascript Node.js + Express.js 提供静态文件的速度非常慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26106399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:30:52  来源:igfitidea点击:

Node.js + Express.js very slow serving static files

javascriptnode.jsexpress

提问by Chris

Sometimes when developing my current node project I will get a hanging refresh. Where the page will never load. I check the network tab in Chrome and see that its always hung up on a static files. The static file that gets stuck will differ, sometimes it will be a CSS file other times an image file.

有时在开发我当前的节点项目时,我会得到一个悬而未决的刷新。页面永远不会加载的地方。我检查了 Chrome 中的网络选项卡,发现它总是挂在静态文件上。卡住的静态文件会有所不同,有时是 CSS 文件,有时是图像文件。

I have tried to optimize all my files in hopes to resolve this issue but nothing has fixed it. If I hit refresh during a long load it will load the page correctly on the 2nd request. This does not happen every time I try to load the page but very often when switching between pages.

我试图优化我的所有文件,希望能解决这个问题,但没有任何解决办法。如果我在长时间加载期间点击刷新,它将在第二次请求时正确加载页面。这不会在我每次尝试加载页面时发生,但在页面之间切换时经常发生。

If I disable cache under the chrome network inspector, It will almost always happen.

如果我在 chrome 网络检查器下禁用缓存,它几乎总是会发生。

**This is my 1st major node project so I could have made mistakes along the way. **

**这是我的第一个主要节点项目,所以我可能会在此过程中犯错误。**

Entire project is hosted on github: http://github.com/polonel/trudesk

整个项目托管在 github 上:http: //github.com/polonel/trudesk

Example Load times: (Open image in new tab to see full-size)

示例加载时间:(在新选项卡中打开图像以查看全尺寸)

回答by pzagor2

I had exactly the same issue. I just moved to a place with a pretty bad internet connection. Loading time of static files in my node.js app increased to more then 40s per file.

我有完全相同的问题。我刚搬到一个互联网连接很差的地方。我的 node.js 应用程序中静态文件的加载时间增加到每个文件 40 多秒。

I just moved the static middleware

我只是移动了静态中间件

app.use(express.static(__dirname + '/public'));

to the top of app.configure function, before all the other app.* calls.

到 app.configure 函数的顶部,在所有其他 app.* 调用之前。

It's now working significantly faster.

现在它的工作速度明显加快。

回答by Nicholas Smith

I spent about 3 hours last night trying to solve this problem. I found that there was a while statement that was slowing the page down terribly:

我昨晚花了大约 3 个小时试图解决这个问题。我发现有一个 while 语句严重减慢了页面速度:

while ((incomingUsername !== "") && (incomingPassword !== "")){
   newAccount(incomingUsername, incomingPassword);
}
function newAccount(name, password){
    console.log("ACCOUNT REGISTRATION INITIATED");
}

When I commented out the while statement, the page loaded in a matter of seconds.

当我注释掉 while 语句时,页面会在几秒钟内加载。