3

我们使用 Testem 来提供一堆 HTML 文件(模板)。在幕后,Testem 使用 Express 的“res.sendfile”方法将静态文件发送回客户端。在 Mac 机器上,这非常快 - 根据 Chrome 网络跟踪,每个文件需要 1-2 毫秒。然而,在 Ubuntu 机器上,它需要 39 毫秒。

这是在最新的稳定节点 - 0.10.29 上。Testem 正在使用 Express 3.1。

关于可能导致此问题的原因或如何进一步诊断的任何建议?

4

1 回答 1

0

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!

于 2014-06-13T21:18:57.843 回答