forked from Jaxnode-UG/async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo2-node-http-get-example.js
50 lines (46 loc) · 1.59 KB
/
demo2-node-http-get-example.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
const https = require('https');
const setTimeToNewYork = require('./nytime');
const httpsOptions = {
hostname: 'api.meetup.com',
port: 443,
path: '/2/events?&sign=true&group_id=10250862&page=20&key=' + process.env.meetupapi_key,
method: 'GET'
};
let nextMeeting = '';
getNextMeetup(function(err, results) {
console.log(results.name);
});
/*
* This is the original method used for retrieving the next meetup. It used a error first callback
*/
function getNextMeetup(cb) {
var sreq = https.request(httpsOptions, function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
nextMeeting += chunk;
});
response.on('end', function () {
var err = false;
if (nextMeeting && nextMeeting.toString().slice(0, 6) !== '<html>') {
var meetingObject = JSON.parse(nextMeeting);
var meetingArray = meetingObject.results;
// permanent fix for the changing timezone plus moment deprecation fix.
setTimeToNewYork(meetingArray);
nextMeeting = '';
cb(err, meetingArray[0]);
} else {
var meetingObject2 = {};
meetingObject2.results = [{}];
cb(err, meetingObject2.results[0]);
}
});
});
sreq.on('error', function (e) {
console.log('problem with request: ' + e.message);
var meetingObject3 = {};
meetingObject3.results = [{}];
cb(e, meetingObject3.results[0]);
});
sreq.write('data\n');
sreq.end();
}