Skip to content

Instantly share code, notes, and snippets.

@gabrielstuff
Created September 12, 2014 22:27
Show Gist options
  • Save gabrielstuff/ec72728b91ea41f8ffcc to your computer and use it in GitHub Desktop.
Save gabrielstuff/ec72728b91ea41f8ffcc to your computer and use it in GitHub Desktop.
async Meteorjs
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