-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgzipped.js
74 lines (57 loc) · 1.45 KB
/
gzipped.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var http = require('http'),
connect = require('connect'),
httpProxy = require('http-proxy'),
zlib = require('zlib');
var selects = [];
var simpleselect = {};
simpleselect.query = '.b';
simpleselect.func = function (node) {
node.createWriteStream().end('<div>+ Trumpet</div>');
}
selects.push(simpleselect);
// Another select for Content-Encoding displaying
selects.push({
query: '.c',
func: function(node){
node.createWriteStream().end('No Content-Encoding');
}
});
//
// Basic Connect App
//
var app = connect();
var proxy = httpProxy.createProxyServer({
target: 'http://localhost:9000'
})
app.use(require('../')([], selects));
app.use(
function (req, res) {
proxy.web(req, res);
}
);
http.createServer(app).listen(8000);
http.createServer(function (req, res) {
// Create gzipped content
var gzip = zlib.Gzip();
var _write = res.write;
var _end = res.end;
gzip.on('data', function(buf){
_write.call(res, buf);
});
gzip.on('end', function(){
_end.call(res);
});
res.write = function(data){
gzip.write(data);
};
res.end = function(){
gzip.end();
};
// Add Content-Encoding header
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Encoding': 'gzip'
});
res.write('<html><head></head><body><div class="a">Nodejitsu Http Proxy</div><div class="b">& Frames</div><div class="c">Content-Encoding: gzip</div></body></html>');
res.end();
}).listen(9000);