forked from kyeongwan/GDG_QR_Staff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
245 lines (214 loc) · 8.28 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var https = require('https');
var http = require('http');
var fs = require('fs');
var path = require('path');
var meetupRequest = require('request');
var port = process.env.PORT || 3000; //*
http.createServer(function (request, response) {
console.log('request starting...');
console.log("URL : " + request.url);
var requestBody = "";
request.on('data', function(contents) {
requestBody += contents;
});
request.on('end', function() {
console.log(requestBody);
});
var access_Token = "";
var filePath = '.' + request.url;
if (filePath == './')
filePath = './qr_check.html';
if(filePath == './admin')
filePath = './test.html';
if (filePath.indexOf("./OAuth/") == 0){
console.log(filePath);
var https2 = require('https'); // http를 https로 고쳤습니다.
var request = https2.request({
hostname: 'api.meetup.com',
path: '/2/member/self/',
method: 'GET',
headers: {
'Authorization' : 'Bearer ' + filePath.substring(8),
'Content-Type' : 'application/json'
}
}, function(meetupResponse) {
var body = "";
meetupResponse.on('data', function(contents) {
body += contents;
});
meetupResponse.on('end', function() {
console.log(meetupResponse.statusCode);
console.log(JSON.parse(body.toString())); // JSON 파싱을 추가했습니다.
access_Token = JSON.parse(body.toString()).id;
response.writeHead(301,
{Location: 'http://lemonlab.co.kr/gdg/qrcode.html?mail='+access_Token}
);
response.end();
});
});
request.on('error', function(e) {
console.log("Error!", e.message);
});
request.end();
} else if(filePath.indexOf("./api") == 0) {
var param = function(url){
let q= url.split('?'),result={};
if(q.length>=2){
q[1].split('&').forEach((item)=>{
try {
result[item.split('=')[0]]= item.split('=')[1];
} catch (e) {
result[item.split('=')[0]]='';
}
})
}
return result;
}
var paramMap = param(request.url);
console.log(paramMap);
meetupRequest.get('http://api.meetup.com/' + paramMap['groupName'] +'/events/' + paramMap['eventId'] + '/rsvps?key=' + paramMap['apiKey']+ '&fields=answers,pay_status&response=yes', function (error, response2, body) {
console.log('error:', error); // console.log('statusCode:', response2 && response2.statusCode); // Print the response status code if a response was received
// Print the error if one occurred
// console.log('body:', body); // Print the HTML for the Google homepage.
var result = new Array();
if(response2.statusCode == 401) {
var object = new Object();
object.code = "auth error";
result.push(object);
response.writeHead(401, {'Content-Type' : 'application/json; charset=utf-8'});
response.end(JSON.stringify(result), 'utf-8');
return
}
var result = new Array();
var obj = JSON.parse(body);
console.log(obj.length);
for (var i = 0; i < obj.length; i++) {
var object = new Object();
object.name = obj[i].member.name;
object.userID = obj[i].member.id;
object.rsvps = obj[i].response;
object.rsvpedOn = obj[i].created;
object.price = obj[i].pay_status;
try{
object.photo = obj[i].member.photo.thumb_link;
}catch(err){
object.photo = "";
}
try {
object.price = obj[i].pay_status;
}catch(err) {
object.price = "";
}
try{
object.answer = obj[i].answers[0].answer;
}catch(err){
object.answer = "";
}
console.log(object);
result.push(object);
}
response.writeHead(200, {'Content-Type' : 'application/json; charset=utf-8'});
response.end(JSON.stringify(result), 'utf-8');
});
// var http2 = require('http');
// var request = http2.request({
// hostname: 'api.meetup.com',
// path: '/GDG-Campus/events/242668148/rsvps?key=9227e7f83811132d1e5e6a42197510',
// method : 'GET'
// }, function(response2) {
// var body = "";
// console.log("bbb");
// response2.on('data', function(contents) {
// body += contents;
// });
// console.log(body);
// response2.on('end', function() {
// console.log(response2.statusCode);
// response.writeHead(200, {'Content-Type' : 'application/json'});
// response.end(body, 'utf-8');
// });
// });
// request.on('error', function(e) {
// console.log("Error!", e.message);
// });
// console.log("aaa");
}else if(filePath.indexOf("./rsvp") == 0){
var jsonData = "";
request.on('data', function (chunk) {
jsonData += chunk;
});
request.on('end', function () {
console.log(jsonData);
var reqObj = JSON.parse(jsonData);
console.log(reqObj);
reqObj = JSON.parse(reqObj);
var member = reqObj.member;
console.log(member);
var status = reqObj.status;
console.log(status);
var groupName = reqObj.groupName;
console.log(status);
var eventId = reqObj.eventId;
console.log(status);
var apiKey = reqObj.apiKey;
console.log(status);
meetupRequest.post({
url: 'http://api.meetup.com/' + groupName +'/events/' + eventId + '/attendance?key=' + apiKey,
form: {member : reqObj.member, status: reqObj.status},
headers: {
'Content-Type' : 'application/x-www-form-urlencoded'
},
json: true },
function (error333, response333, body333) {
console.log(body333)
response.writeHead(200);
response.end(JSON.stringify(body333));
})
});
}else {
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.svg':
contentType = 'image/svg+xml';
break;
case '.wav':
contentType = 'audio/wav';
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
}).listen(port);