Chris
Chris

Reputation: 315

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. The static file that gets stuck will differ, sometimes it will be a CSS file other times an image file.

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.

**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

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

Upvotes: 14

Views: 16033

Answers (2)

Nicholas Smith
Nicholas Smith

Reputation: 712

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:

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.

Upvotes: 0

pzagor2
pzagor2

Reputation: 719

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.

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.

It's now working significantly faster.

Upvotes: 27

Related Questions