You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+3
Original file line number
Diff line number
Diff line change
@@ -3,3 +3,6 @@
3
3
This incredibly simple script provides a HTTP server used to forward the request to one or more servers. For this you need to provide a DNS name in the env variable `TARGET_HOSTS_DNS_NAME`. For each ip address to which this hostname resolves, this script issues a request on the port defined in `TARGET_HOSTS_PORT` and the given method and path from the original request.
4
4
5
5
Use case for this is to forward a http trigger to all instances of a deployment in a kubernetes cluster in parallel. The idea is to use a headless service for this case to retrieve all pods ip addresses by querying the hostname of this headless service.
6
+
7
+
Additionally, you can specify the env variable `TARGET_ADDITIONAL_FORWARD_ENDPOINTS` to add additional endpoints, to which the request should be forwarded. It accepts a comma separated list of URL and method combinations like `http://your-endpoint:1234/test;POST`.
returnnext(newError("Unable to resolve hostname '"+process.env.TARGET_HOSTS_DNS_NAME+"'."));
27
49
}
28
50
29
51
constpromises=[];
30
52
31
-
addresses.forEach(address=>{
32
-
promises.push(newPromise((res,rej)=>{
33
-
if(req.headers.host!=undefined){
53
+
addresses.forEach((address)=>{
54
+
promises.push(newPromise((resolve,reject)=>{
55
+
if(req.headers.host!==undefined){
34
56
deletereq.headers.host;
35
57
}
36
58
constoptions={
37
59
hostname: address.address,
38
60
port: process.env.TARGET_HOSTS_PORT,
39
61
path: req.url,
40
62
method: req.method,
41
-
headers: req.headers
42
-
}
63
+
headers: req.headers,
64
+
};
43
65
44
-
console.log("Sending %s request to http://%s:%d%s with headers: %s",options.method,options.hostname,options.port,options.path,JSON.stringify(options.headers));
66
+
console.log(
67
+
"Sending %s request to http://%s:%d%s with headers: %s",
68
+
options.method,
69
+
options.hostname,
70
+
options.port,
71
+
options.path,
72
+
JSON.stringify(options.headers)
73
+
);
45
74
46
75
http
47
-
.request(options,resp=>{
76
+
.request(options,(resp)=>{
48
77
constchunks=[];
49
-
resp.on('data',data=>chunks.push(data));
78
+
resp.on('data',(data)=>chunks.push(data));
50
79
resp.on('end',()=>{
51
80
letresBody=Buffer.concat(chunks);
52
81
switch(resp.headers['content-type']){
53
82
case'application/json':
54
83
resBody=JSON.parse(resBody);
55
84
break;
56
85
}
57
-
console.log("Got response for call: http://%s:%d%s -> %d: %s",options.hostname,options.port,options.path,resp.statusCode,resp.statusMessage);
58
-
res({
86
+
console.log(
87
+
"Got response for call: http://%s:%d%s -> %d: %s",
88
+
options.hostname,
89
+
options.port,
90
+
options.path,
91
+
resp.statusCode,
92
+
resp.statusMessage
93
+
);
94
+
resolve({
59
95
...options,
60
96
statusCode: resp.statusCode,
61
97
statusMessage: resp.statusMessage,
62
-
body: resBody.toString()
63
-
})
64
-
65
-
})
98
+
body: resBody.toString(),
99
+
});
100
+
});
66
101
})
67
-
.on("error",err=>{
68
-
console.log("Error: "+err.message);
69
-
// Promise.all does not work with errors
70
-
res({
102
+
.on('error',(err)=>{
103
+
console.log('Error: '+err.message);
104
+
resolve({
71
105
...options,
72
-
err: err
73
-
})
106
+
err: err,
107
+
});
74
108
})
75
109
.end();
76
110
}));
77
111
});
78
112
79
-
Promise
80
-
.all(promises)
113
+
endpointsList.forEach((endpoint)=>{
114
+
promises.push(newPromise((resolve,reject)=>{
115
+
constoptions={
116
+
hostname: endpoint.hostname,
117
+
port: endpoint.port,
118
+
path: endpoint.path,
119
+
method: endpoint.method,
120
+
headers: req.headers,
121
+
insecureHTTPParser: true
122
+
};
123
+
124
+
console.log(
125
+
"Sending %s request to http://%s:%d%s with headers: %s",
126
+
options.method,
127
+
options.hostname,
128
+
options.port,
129
+
options.path,
130
+
JSON.stringify(options.headers)
131
+
);
132
+
133
+
constreqForward=http.request(options,(resp)=>{
134
+
constchunks=[];
135
+
resp.on('data',(data)=>chunks.push(data));
136
+
resp.on('end',()=>{
137
+
letresBody=Buffer.concat(chunks);
138
+
switch(resp.headers['content-type']){
139
+
case'application/json':
140
+
resBody=JSON.parse(resBody);
141
+
break;
142
+
}
143
+
console.log(
144
+
"Got response for call: http://%s:%d%s -> %d: %s",
0 commit comments