Created
September 12, 2014 22:27
-
-
Save gabrielstuff/ec72728b91ea41f8ffcc to your computer and use it in GitHub Desktop.
async Meteorjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router.map(function() { | |
this.route('messages', { | |
path: '/api/messages', | |
where: 'server', | |
action: function() { | |
xml2js = Meteor.npmRequire('xml2js'); | |
// GET, POST, PUT, DELETE | |
var req = this.request; | |
var res = this.response; | |
var post_data = ""; | |
req.on('data', function(chunk) { | |
post_data += chunk; | |
}); | |
req.on('end', function() { | |
console.log(post_data); | |
var parser = new xml2js.Parser(); | |
try { | |
parser.parseString(post_data, function(err, sms) { | |
console.log(sms.InboundMessage.MessageText[ | |
0]); | |
console.log(sms.InboundMessage.From[0]); | |
Messages.insert({ | |
message: sms.InboundMessage.MessageText[ | |
0], | |
number: sms.InboundMessage.From[ | |
0] | |
}, function(err, id) { | |
if (err) { | |
throw "oops:" + err; | |
} | |
res.writeHead(200, { | |
'Content-Type': 'text/plain' | |
}); | |
res.end("OK"); | |
}); | |
}); | |
} catch (err) { | |
res.writeHead(400, { | |
'Content-Type': 'text/plain' | |
}); | |
res.end("NOK - "+err); | |
console.log("Wrong data type !"); | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment