使用node.js服务静态gzip文件 [英] Serve static gzip files using node.js

查看:115
本文介绍了使用node.js服务静态gzip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于开销太大,我不想使用随即gzip的库.

I don't want to use a library which gzips on the fly, because of the overhead.

该网站具有一些动态组件,这些组件在node.js中实现.

The website has some dynamic components which is implemented in node.js.

我有一些静态的js和css文件,以及它们压缩后的对应文件.我只想将gzip压缩版本提供给支持它的浏览器.

I have some static js and css files as well as their gzipped counterparts. I want to serve the gzipped version only to browsers which support it.

我考虑过在express中使用静态中间件来提供静态文件,以及一些URL重写中间件来有条件地提供压缩文件.但是,我找不到任何条件重写模块.

I considered using the static middleware in express to serve the static files, along with some URL rewriting middleware to conditionally serve the gzipped files. However, I cannot find any conditional rewrite module.

我无法相信没有人做到这一点,或者它需要这么多的解决方法.我想念什么?

I cannot believe that no one has done this, or that it needs so many work arounds. What am I missing?

另一方面,通过node.js提供静态文件是否太昂贵了?另一方面,将Apache用于静态文件并在其后运行node.js似乎也很糟糕. AWS EC2托管的最愚蠢配置是什么?

On a different note, is serving up static files via node.js too expensive? On the other hand, using Apache for static files and running node.js behind seems bad as well. What is the least stupid configuration for AWS EC2 hosting?

推荐答案

如果要为客户提供最佳性能,只需使用CDN.它将为您和其他很多东西提供gzip压缩功能.如果您需要帮助,可以使用 express-cdn 模块.

If you want to have best performance for your clients, just use a CDN. It will take care of gzipping for you and a lot of other stuff. If you need help you can use express-cdn module.

如果由于某些原因而不喜欢CDN,最好的选择是使用nginx.我在您的问题中看到了它的标签,但您对此一无所获. nginx比node.js快得多.对于nginx,您可以检查它的 gzip静态模块.

If you don't like CDNs for some reasons, your best bet is using nginx. I see it tagged in your question, but you didn't mention anything about it. nginx is way faster than node.js. For nginx you can check it's gzip-static module.

如果您仍要使用node.js,请 connect-gzip-static 是你最好的选择.它的工作原理几乎与nginx-gzip-static模块相同.如果请求的文件有.gz个文件对应文件,它将使用该文件,否则将回退到普通的connect-static模块.

If you still want to use node.js, then connect-gzip-static is your best bet. It works almost same as nginx-gzip-static module. If there's a .gz file counterpart of the requested file, then it will use that, else it will fallback to normal connect-static module.

不要忘记事先编译文件,如果您使用的是gulp,那么您也可以使用 gulp- gzip .如果不是,请仅使用gzip命令.

Don't forget to compile files beforehand, if you are using gulp, then you might also use gulp-gzip. If not just use gzip command.

还有 gzippo ,它可以即时进行gzip压缩,但也可以将结果缓存在内存中.因此,它将只压缩一次.

There's also gzippo, it gzips on the fly, but it also caches the result in the memory. So it will be only gzipped once.

这篇关于使用node.js服务静态gzip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆