-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (40 loc) · 1.33 KB
/
index.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
const http = require('http');
const url = require('url');
const API_HOSTNAME = process.env.SVC_API_HOSTNAME || 'node-app.api.svc.cluster.local';
const API_PORT = parseInt(process.env.SVC_API_PORT || '4000');
function options(url) {
return {
hostname: API_HOSTNAME,
port: API_PORT,
path: url,
method: 'GET',
agent: new http.Agent({ keepAlive: true, maxSockets: 2000, scheduling: 'fifo' })
}
}
http.createServer(async function (req, res) {
//console.log(req.headers);
//console.log(req.method, req.url);
let url_parts = url.parse(req.url, true);
let query = url_parts.query;
const request = http.request(options('/?number=' + query.number), response => {
//console.log(`statusCode: ${response.statusCode}`)
let data = '';
response.on('data', chunk => {
data += chunk;
})
response.on('end', () => {
res.setHeader('Content-Type', 'application/json');
res.write(data.replace('}', ', "test": "nuevo14" }'));
res.end();
});
})
request.on('error', error => {
console.error(error)
res.writeHead(500);
res.write('Error request to api');
res.end();
})
request.end();
//end the response
}).listen(5000);
console.log(`Worker ${process.pid} started`);