forked from evolvingweb/node-proxies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (32 loc) · 923 Bytes
/
app.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
var httpProxy = require('http-proxy');
var url = require('url');
httpProxy.createServer(function(req, res, proxy) {
var isHtml = false,
write = res.write,
writeHead = res.writeHead,
params = url.parse(req.url, true).query;
delete req.headers['accept-encoding'];
res.writeHead = function(code, headers) {
isHtml = (headers['content-type'] == 'text/html');
writeHead.apply(this, arguments);
}
res.write = function(data, encoding) {
if (isHtml) {
var str = data.toString();
var i = 0;
str = str.replace(/<(h[0-9][^>]*)>/g, function(str, match) {
if (i == params.header) {
str = '<' + match + ' id="header-jump">';
}
i += 1;
return str;
});
data = new Buffer(str);
}
write.call(this, data, encoding);
};
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 80,
});
}).listen(9000);