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?
I typically serve static files directly using:
app.use( express.static(__dirname+'/public') );
middleware. Your static files would be stored in
This will allow you to access
If you put
/<app-path>/public/html/, the following would resolve:
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
jade may help? It would allow you to use the
The thing is, I have seen request times increase when using:
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!