-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionRev.js
More file actions
339 lines (334 loc) · 8.62 KB
/
functionRev.js
File metadata and controls
339 lines (334 loc) · 8.62 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"use strict";
var http = require('http');
var mongoose = require('mongoose');
var fs = require('fs');
var qs = require('querystring');
var connect = require('connect');
var app = connect();
var serveStatic = require('serve-static');
var keyIn ;
var accountSid = '';
var authToken = '';
var client = require('twilio')(accountSid, authToken);
mongoose.connect('mongodb://localhost/my_database');
var lingSchema = new mongoose.Schema({
from: String,
to: String,
name: String,
title: String,
body: String,
address: String,
number: String,
comment: [{
body: String,
date: Date
}],
date: {
type: Date,
default: Date.now
}
});
var Email = mongoose.model('Email', lingSchema);
var emailArray = new Array();
for (var i = 0; i < 10; i++) {
var email = new Email();
email["from"] = 'Joy ' + i.toString() + '@gmail.com';
email["to"] = 'Ling ' + i.toString() + '@gmail.com';
email["name"] = 'Ling ' + i.toString();
email["title"] = 'Greeting ' + i.toString();
email["body"] = 'How are you! ' + i.toString();
email["address"] = 'My Address is ' + i.toString();
email["number"] = 'My number is ' + i.toString();
emailArray.push(email);
}
console.log('Email' + emailArray);
for (var j = 0; j < 10; j++) {
emailArray[j].save(function(err) {
if (err) return console.error(err);
});
}
console.log('finished');
app.use (function(req, res, next) {
function myInput(file, code, option, next) {
fs.readFile(file, {
encoding: "utf8"
}, function(err, data) {
res.writeHead(code, {
'Content-Type': option
});
if (err) throw err;
res.write(data);
if (next) {
next(function(err) {
if (err) throw err;
});
} else {
res.end();
}
});
};
var twCall = function(inputNum) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
client.calls.create({
to: inputNum,
from: "+16504379899",
url: "http://demo.twilio.com/docs/voice.xml",
applicationSid: "APa894ccc18916d5496c35bbe7bd7f07bc",
method: "GET",
fallbackMethod: "GET",
statusCallbackMethod: "GET",
record: "false"
}, function(err, call) {
if (err) return console.error(err);
console.log(call.sid);
res.write("<h1>" + "Call Section ID:" + call.sid + "</h1>");
res.end();
});
};
var twText = function(inputNum) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
client.messages.create({
body: "This is Ling",
to: inputNum,
from: "+16504379899"
}, function(err, message) {
if (err) return console.error(err);
console.log(message.sid);
res.write("<h1>" + "Message Section ID:" + message.sid + "</h1>");
res.end();
});
};
if (req.url === "/index.html") {
console.log('hello');
debugger;
myInput("Emailvalid.html", "200", "text/html", function(cb) {
console.log("hello");
fs.readFile("httpHeader.html", {
encoding: "utf8"
}, function(err, data) {
res.write('<h1>Email HomePage</h1>');
Email.find({
name: {
$regex: 'Ling 8'
},
}, function(err, found) {
if (err) return console.error(err);
console.log('err check');
if (found) {
for (var k = 0; k < found.length; k++) {
res.write("<h2>" + "From: " + found[k].from + "</h2>");
res.write("<h2>" + "Body: " + found[k].body + "</h2>");
}
};
fs.readFile("httpFooter.html", {
encoding: "utf8"
}, function(err, data) {
res.write("<h1>Finished my email test</h1>");
cb(null);
res.end();
});
});
});
});
} else if (req.url === "/call") {
twCall(keyIn.number);
} else if (req.url === "/text") {
twText(keyIn.number);
} else if (req.url === "/action") {
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
keyIn = qs.parse(chunk.toString());
console.log(keyIn);
debugger;
keyIn = new Email(keyIn);
keyIn.save(function(err) {
if (err) return console.error(err)
console.log(err);
console.log(keyIn.address);
console.log(keyIn.number);
});
});
req.on('end', function() {
debugger;
fs.readFile("httpHeader.html", {
encoding: 'utf8'
}, function(err, data) {
res.writeHead(200, "OK", {
'Content-Type': 'text/html'
});
if (err) throw err;
res.write(data);
res.write('<h1>New Input</h1>');
Email.find({
address: {
$regex: '.com'
},
number: {
$regex: ''
},
}, function(err, found) {
debugger;
if (err) return console.error(err);
console.log('EM Address');
if (found) {
for (var n = 0; n < found.length; n++) {
res.write("<h2>" + "User Input Address : " + found[n].address + "</h2>");
res.write("<h1>" + "User Input Number : " + found[n].number + "</h1>");
res.write("<a class='btn btn-default' href='call' role= 'button'>Call Now</a>");
res.write("<a class='btn btn-default' href='text' role= 'button'>Text Now</a>");
};
res.end("<h1>Finish Ling Mail</h1>");
};
});
fs.readFile("httpFooter.html", {
encoding: "utf8"
}, function(err, data) {
res.write("<h1>Finished my email test</h1>");
});
});
});
} else {
next();
}
});
app.use('/action', function action (req, res, next) {
if (req.url === "/action") {
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
keyIn = qs.parse(chunk.toString());
console.log(keyIn);
debugger;
keyIn = new Email(keyIn);
keyIn.save(function(err) {
if (err) return console.error(err)
console.log(err);
console.log(keyIn.address);
console.log(keyIn.number);
});
});
req.on('end', function() {
debugger;
fs.readFile("httpHeader.html", {
encoding: 'utf8'
}, function(err, data) {
res.writeHead(200, "OK", {
'Content-Type': 'text/html'
});
if (err) throw err;
res.write(data);
res.write('<h1>New Input</h1>');
Email.find({
address: {
$regex: '.com'
},
number: {
$regex: ''
},
}, function(err, found) {
debugger;
if (err) return console.error(err);
console.log('EM Address');
if (found) {
for (var n = 0; n < found.length; n++) {
res.write("<h2>" + "User Input Address : " + found[n].address + "</h2>");
res.write("<h1>" + "User Input Number : " + found[n].number + "</h1>");
res.write("<a class='btn btn-default' href='call' role= 'button'>Call Now</a>");
res.write("<a class='btn btn-default' href='text' role= 'button'>Text Now</a>");
};
res.end("<h1>Finish Ling Mail</h1>");
};
});
fs.readFile("httpFooter.html", {
encoding: "utf8"
}, function(err, data) {
res.write("<h1>Finished my email test</h1>");
});
});
});
};
next ();
});
/*app.use('/call', function callMiddleware(req, res, next){
function twCall (inputNum) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
client.calls.create({
to: inputNum,
from: "+16504379899",
url: "http://demo.twilio.com/docs/voice.xml",
applicationSid: "APa894ccc18916d5496c35bbe7bd7f07bc",
method: "GET",
fallbackMethod: "GET",
statusCallbackMethod: "GET",
record: "false"
}, function(err, call) {
if (err) return console.error(err);
console.log(call.sid);
res.write("<h1>" + "Call Section ID:" + call.sid + "</h1>");
res.end();
});
};
next();
});
app.use(function myWrapper(req, res, next) {
function myInput(file, code, option, next) {
fs.readFile(file, {
encoding: "utf8"
}, function(err, data) {
res.writeHead(code, {
'Content-Type': option
});
if (err) throw err;
res.write(data);
if (next) {
next(function(err) {
if (err) throw err;
});
} else {
res.end();
}
});
};
});
app.use('/index.html', function printDatabase(req, res, next){
myInput("Emailvalid.html", "200", "text/html", function(cb) {
console.log("hello");
fs.readFile("httpHeader.html", {
encoding: "utf8"
}, function(err, data) {
res.write('<h1>Email HomePage</h1>');
Email.find({
name: {
$regex: 'Ling 8'
},
}, function(err, found) {
if (err) return console.error(err);
console.log('err check');
if (found) {
for (var k = 0; k < found.length; k++) {
res.write("<h2>" + "From: " + found[k].from + "</h2>");
res.write("<h2>" + "Body: " + found[k].body + "</h2>");
}
};
fs.readFile("httpFooter.html", {
encoding: "utf8"
}, function(err, data) {
res.write("<h1>Finished my email test</h1>");
cb(null);
res.end();
});
});
});
});
next();
});*/
app.use(serveStatic("public", {'index': ['panels.html']}));
http.createServer(app).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');