-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
74 lines (70 loc) · 2.26 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
/* Twillio App */
var http = require('http');
var meryl = require('meryl');
var fs = require('fs');
var https = require('https');
var url = require('url');
var formidable = require('formidable');
var express = require('express');
meryl.handle('GET /',function(req,res){
// fs.readFile('./index.html',encoding='utf8',function(err,data){
// if(err) console.log(err);
res.writeHead(200,{'Content-Type':'text/html'});
res.end(
'<form action="/makecall" method="post">'+
'<input type="text" name="from"><br>'+
'<input type="text" name="to"><br>'+
'<input type="submit" value="Submit">'+
'</form>'
);
// });
});
meryl.handle('GET /recievedcall', function(req,res){
console.log(req.params);
fs.readFile('./recieveform.xml',encoding='utf8',function(err,data){
if(err) console.log(err);
console.log(data);
var resarray = {from:req.params.From}
substitute(data,resarray,function(html){
res.setHeader('Content-Type','text/xml');
res.end(html);
console.log(html);
});
});
});
meryl.handle('POST /makecall',function(req,res){
console.log('making call...');
console.log(req);
var options = {
host: 'ACacb4ecc7916a22d1eaefcc880b616f02:[email protected]',
port: 80,
path: '/2010-04-01/Accounts/ACacb4ecc7916a22d1eaefcc880b616f02/CallsFrom='+ req.params.from+ '&TO=' + req.params.to + '&Url=http://ec2-50-16-59-162.compute-1.amazonaws.com/recievedcall',
method: 'POST'
}
console.log(options);
var request = https.request(options, function(response){
console.log('statusCode: ',response.statusCode);
console.log('headers: ',response.headers);
response.on('data',function(data){
process.stdout.write(data);
});
});
request.end();
request.on('error', function(err){
console.log(err);
});
});
function substitute(string,array,callback){
var re = /<:\s(\w+)\s:>/g;
var searchString = string;
var result = searchString.match(re);
for (i=0;i<result.length;i++){
var reg = /\s(\w+)\s/;
var tarray = reg.exec(result[i]);
var templateName = tarray[1];
var replacement = array[templateName];
searchString = searchString.replace(result[i],replacement);
}
callback(searchString);
};
meryl.run({debug:true, port: 3000});