简体   繁体   English

Node.js + Express.js服务静态文件非常慢

[英]Node.js + Express.js very slow serving static files

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. 我检查了Chrome中的网络标签,看到它总是挂在静态文件上。 The static file that gets stuck will differ, sometimes it will be a CSS file other times an image file. 卡住的静态文件会有所不同,有时它会成为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) 示例加载时间:(在新选项卡中打开图像以查看完整大小)

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。*调用之前,在app.configure函数的顶部。

It's now working significantly faster. 现在它的工作速度明显加快了。

I spent about 3 hours last night trying to solve this problem. 我昨晚花了大约3个小时试图解决这个问题。 I found that there was a while statement that was slowing the page down terribly: 我发现有一段时间的声明会严重降低页面的速度:

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语句时,页面会在几秒钟内加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM