Express.js:如何压缩资产 [英] Express.js: how to get assets gzipped

查看:78
本文介绍了Express.js:如何压缩资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用compress()中间件,将其放在configure()中的第一位.

I use compress() middleware, put it the first in configure().

app.configure('all', function(){
  app.use(express.compress());
  ...
  app.use(express.static('public'), { maxAge: oneMonth });
})

如何检查我的内容是否已压缩?我有一个很奇怪的情况:1)在我的开发机器上:我需要localhost:4000/mystyle.css-看不到内容编码:gzip

How do I check that my content is gzipped? I've got a fricking strange situation: 1) On my dev machine: I reqeust localhost:4000/mystyle.css - DON'T see Content-encoding: gzip

2)当我将其部署到生产环境中时,如果我自己请求文件mydomain.com/mystyle.css-我在那里看到了内容编码:gzip

2) When I deploy it on production if I request the file it self mydomain.com/mystyle.css - I SEE there see Content-encoding: gzip

3)我请求mydomain.com,并在chrome开发工具中的网络"中查看,在那里找到mystyle.css,在那儿我看不到内容编码:gzip

3) I request mydomain.com and see in Network in chrome dev tools, find there mystyle.css and there I DON'T see Content-encoding: gzip

4)我使用不同的服务来检查我的内容是否被压缩,有人说是,有些则不是.

4) I use different services to check if my content is gzipped some says that it IS, some that it IS NOT.

WTF?有人可以解释吗?

WTF? Can some one explain?

推荐答案

您的问题是您对app.configure的使用.不推荐使用此功能,但是您正在专门使用它,是为了寻找 all 环境.

Your issue is your use of app.configure. This is largely deprecated, but you're specifically using it such that you're looking for an all environment.

文档解释:此方法出于遗留原因而保留,实际上是一种如以下代码段所示的if语句."

The documentation explains: "This method remains for legacy reason, and is effectively an if statement as illustrated in the following snippets."

相反,只需使用app.use而不将它们包装在configure语句中.

Instead, just use the app.use without wrapping them in a configure statement.

这篇关于Express.js:如何压缩资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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