Slow static file serving with Node on Ubuntu

We’re using Testem to serve a bunch of HTML files (templates). Under the covers Testem uses the “res.sendfile” method of Express to send the static file back to the client. On Mac machines this is very fast – 1-2 ms per file according to the Chrome network trace. On an Ubuntu machine, however, it takes 39ms.

This is on the latest stable Node – 0.10.29. Testem is using Express 3.1.

Any suggestions on what might cause this or how I can diagnose it further?

Add Comment
1 Answer(s)

I typically serve static files directly using:

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

middleware. Your static files would be stored in

/<app-path>/public

This will allow you to access /<app-path>/public/some.html at:

http://yoursite.com/some.html

If you put file.html in /<app-path>/public/html/, the following would resolve:

http://yoursite.com/html/file.html

http://yoursite.com/public/html/file.html

If the desired result is to have clean urls without extensions, then my suggestion will not do. However, if you don’t mind file extensions within urls, the static middleware should reduce request times, maybe even dramatically. Also, maybe a templating engine like dust or jade may help? It would allow you to use the res.render fn.

The thing is, I have seen request times increase when using:

res.sendfile(somepath +'/some.html');

Because express will pass that through its regex path resolution middleware before serving the file. If you have a ton of routes, that may also be slowing down request times.

Hope that helps!

Answered on July 11, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.