This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
76 lines (66 loc) · 2.1 KB
/
server.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
71
72
73
74
75
76
/* eslint-disable no-console */
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const { handler } = require('./index');
const PORT = 3000;
const eventRequestGenerator = (httpMethod, path, body, headers) => {
const parsedPath = url.parse(path);
const queryStringParameters = querystring.parse(parsedPath.query);
return {
httpMethod,
path: parsedPath.pathname,
queryStringParameters,
requestContext: {
elb: {
targetGroupArn:
'arn:aws:elasticloadbalancing:us-east-1:901194531837:targetgroup/panamah-dashboard-hom-target-gp/2c6ab79f901cf5fb',
},
},
headers: {
accept: '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'access-control-request-headers': 'authorization',
'access-control-request-method': 'GET',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'x-amzn-trace-id': 'Root=1-5df50966-fcef36da4eaf8f4abedb80ea',
'x-forwarded-for': '191.6.8.217',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https',
...(headers || {}),
},
body: body || 'null',
isBase64Encoded: false,
};
};
const getBody = async (req) =>
new Promise((resolve) => {
let body = [];
req
.on('data', (chunk) => {
body.push(chunk);
})
.on('end', () => {
body = Buffer.concat(body).toString();
resolve(body);
});
});
const server = http.createServer(async (req, res) => {
const label = `${new Date().toISOString()} | [${req.method}] ${req.url}`;
console.time(label);
const body = await getBody(req);
const event = eventRequestGenerator(req.method, req.url, body, req.headers);
const response = await handler(event, {});
res.writeHead(response.statusCode, response.headers);
if (response.isBase64Encoded) {
res.end(Buffer.from(response.body, 'base64'));
} else {
res.end(response.body);
}
console.timeEnd(label);
});
server.listen(PORT, () => {
console.log(`O endpoit de testes foi iniciado na porta ${PORT} | http://localhost:${PORT}`);
});