nodejs反向代理

正向代理代理客户端,反向代理代理服务器。
使用nodejs实现简单的反向代理需要
1、创建一个http服务
2、服务接收到客户端请求后,去请求目的url
3、获取请求结果,返回给客户端

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
var http = require('http');
var https = require('https');
var path = require('path');
console.log('run')
// Create HTTP Server
http.createServer(function(req, res) {
var uri = 'https://github.com/' + req.url;
var file = req.url.replace(/\?.*/ig, '');
var ext = path.extname(file);
var type = getContentType(ext);
res.writeHead(200, {
'Content-Type': type
});
https.get(uri, function(response) {
response.addListener('data', function(chunk)
{
res.write(chunk, 'binary');
});
response.addListener('end', function()
{
res.end();
});
});
}).listen(3000);
// Get content-type
var getContentType = function(ext) {
var contentType = '';
switch (ext) {
case "":
contentType = "text/html";
break;
case ".html":
contentType = "text/html";
break;
case ".js":
contentType = "text/javascriptss";
break;
case ".css":
contentType = "text/css";
break;
case ".gif":
contentType = "image/gif";
break;
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".ico":
contentType = "image/icon";
break;
default:
contentType = "application/octet-stream";
}
return contentType;
};