我创建了一个小应用程序其余的node.js.我尝试压缩数据json,该数据通过请求api返回,但是没有压缩.使用快速和压缩.
var express = require('express');
var methodoverride = require('method-override');
var bodyParser = require('body-parser');
var serveStatic = require('serve-static');
var compression = require('compression');
var app = express();
app.use(compression());
app.use(methodoverride('X-HTTP-Method-Override'));
app.use(bodyParser.json());
app.use(serveStatic('public', {'index': ['index.html']}));
app.use('/', require('./routes'));
app.use(function(req, res) {
res.sendfile('public/index.html');
});
app.disable('x-powered-by');
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
});
这个没有压缩的响应头=(((
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 3756
ETag: W/"56IqvwOVCBB3MRndvDsFTA=="
vary: Accept-Encoding
Date: Wed, 10 Jun 2015 14:21:11 GMT
Connection: keep-alive
救命.
解决方法:
您是否使用普通卷发进行请求?告诉curl请求压缩数据:
$curl -H 'Accept-Encoding: gzip,deflate' -v -o tmp http://localhost:3000/
* Connected to localhost (::1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:3000
> Accept: */*
> Accept-Encoding: gzip,deflate
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< ETag: W/"qIOLMe2deSCB3/8ol7nulg=="
< vary: Accept-Encoding
< content-encoding: gzip # <========================================== !!!
< Date: Wed, 10 Jun 2015 16:14:50 GMT
< Connection: keep-alive
< transfer-encoding: chunked
现在下载已压缩.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。