最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - compress json data from rest node.js use express compression - Stack Overflow

programmeradmin0浏览0评论

I created a small application rest node.js. I try to press data json, which return by request api, but nothing pressed. Use express and pression.

var express = require('express');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var serveStatic = require('serve-static');
var pression = require('pression');

var app = express();
app.use(pression());

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;
});

this response header without pession =(((

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

Help.

I created a small application rest node.js. I try to press data json, which return by request api, but nothing pressed. Use express and pression.

var express = require('express');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var serveStatic = require('serve-static');
var pression = require('pression');

var app = express();
app.use(pression());

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;
});

this response header without pession =(((

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

Help.

Share Improve this question edited May 18, 2018 at 11:43 Rahul Kumar 5,2295 gold badges35 silver badges44 bronze badges asked Jun 10, 2015 at 15:05 kamerrerkamerrer 611 silver badge2 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Are you using plain curl to request it? Tell curl to ask for pressed data:

$ 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

Now the download is gzipped.

发布评论

评论列表(0)

  1. 暂无评论