-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-request-get.js
More file actions
51 lines (44 loc) · 1.34 KB
/
http-request-get.js
File metadata and controls
51 lines (44 loc) · 1.34 KB
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
// Here we can use http or https module
const https = require('https');
exports.handler = (event, context, callback) => {
// normal way
// https.get(process.env.ENDPOINT, (res) => {
// res.setEncoding('utf8');
// let rawData = '';
// res.on('data', (chunk) => { rawData += chunk; });
// res.on('end', () => {
// try {
// console.log(rawData);
// } catch (e) {
// console.error(e.message);
// }
// callback(null, 'success msg')
// });
// }).on('error', (e) => {
// console.error(`Got error: ${e.message}`);
// callback(new Error(e.message))
// });
// async await
let dataString = '';
const response = await new Promise((resolve, reject) => {
const req = https.get(process.env.ENDPOINT, function(res) {
res.on('data', chunk => {
dataString += chunk;
});
res.on('end', () => {
resolve({
statusCode: 200,
body: dataString
});
});
});
req.on('error', (e) => {
reject({
statusCode: 500,
body: 'Something went wrong!'
});
});
});
return response;
};
// We then can use Cloudwatch event to run this function, for example: 0 16 * * ? *