-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.js
70 lines (64 loc) · 1.82 KB
/
proxy.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
const http = require('http');
const url = require('url');
/**
* 如果你要准备ueditor和ReactUEditorComponent同时开发联调
* 一下的配置可以解决两个项目在不同端口下运行时,iframe产生的跨域问题
* 将ueditor运行在8080端口,/utf8-phplu路径
* 将ReactUEditorComponent运行在3000端口
* 浏览器访问8000端口
*/
let server = http.createServer((req, res) => {
const path = url.parse(req.url).path;
console.log(path);
if (path.startsWith('/utf8-php')) {
let request = http.request({
port: 8080,
path,
method: req.method,
headers: req.headers
}, (response) => {
res.writeHead(response.statusCode, response.headers);
response.pipe(res);
});
req.pipe(request);
} else if (!path.includes('websocket')) {
let request = http.request({
port: 3000,
path,
method: req.method,
headers: req.headers
}, (response) => {
res.writeHead(response.statusCode, response.headers);
response.pipe(res);
});
req.pipe(request);
} else {
res.end();
}
}).listen(8000);
// websocket转发
server.on('upgrade', (req, client) => {
const path = url.parse(req.url).path;
let request = http.request({
port: 3000,
path,
headers: req.headers
});
request.on('upgrade', (res, socket) => {
client.write(formatProxyResponse(res));
client.pipe(socket);
socket.pipe(client);
});
request.end();
});
function formatProxyResponse (res) {
const headers = res.headers;
const keys = Object.getOwnPropertyNames(headers);
let switchLine = '\r\n';
let response = [`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}${switchLine}`];
keys.forEach((key) => {
response.push(`${key}: ${headers[key]}${switchLine}`);
});
response.push(switchLine);
return response.join('');
}