问题描述
我一直在努力提高网站的性能,并被告知在将所有内容发送到浏览器之前,我应该先对其进行gzip压缩.
I've been trying to speed up my websites performance and was told that I should gzip everything stuff before they are sent to the browser.
我发现的解决方案(希望能起作用)是包含内容编码标头.
The solution I found(which I hope works) is to include the content-encoding header.
在我的app.js文件的底部,我有以下代码
At the bottom of my app.js file I have this code
http.createServer(app).listen(app.get('port'), function(req,res){
console.log('Express server listening on port ' + app.get('port'));
res.setHeader("Content-Encoding","gzip");
});
我收到这样的错误:
C:\Users\user\workspace\site\app.js:35
res.setHeader("Content-Encoding","gzip");
^
TypeError: Cannot call method 'setHeader' of undefined
您能解释一下这里出了什么问题吗?据我所知,该代码具有正确的语法.有什么解决方案.
Could you explain what is going wrong here? From what I know, the code has the proper syntax. What's the solution.
此外,这将加快我的网站的速度吗?(我不确定)
Also, this will speed up my website correct? (I'm not sure)
我知道的另一件事是标题不应在文本之前发送.我认为这可能是我的问题
Another thing I know of is that headers should not be sent before the text. I think that may be my issue
推荐答案
传递给 listen()
的回调没有任何参数,仅当服务器绑定到端口并且然后在听.
The callback passed to listen()
does not have any parameters, it is only called when the server has bound to the port and is then listening.
对于响应压缩,您可能会考虑使用压缩中间件,而不是自己尝试做.
For response compression, you might look at using the compression middleware instead of trying to do it yourself.
这篇关于如何在node.js中设置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!