Last active
August 29, 2015 14:09
-
-
Save linus-amg/a2c2da706d937dffd0ea to your computer and use it in GitHub Desktop.
XML to JSON
This file contains 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
var util = require('util'); | |
var parser = require('xml2json'); | |
var express = require('express'); | |
var app = express(); | |
app.use(anyBodyParser); | |
app.post('/xml_to_json', function (req, res) { | |
var json = parser.toJson(req.rawBody, { arrayNotation: true }); | |
res.send(json); | |
}); | |
app.post('/xml_to_xml', function (req, res) { | |
var json = parser.toJson(req.rawBody, { arrayNotation: true }); | |
/* | |
modify the json here and send it back as XML | |
*/ | |
var xml = parser.toXml(json); | |
res.send(xml); | |
}); | |
var server = app.listen(4000, function () { | |
var host = server.address().address; | |
var port = server.address().port; | |
console.log('Escuchando en http://%s:%s', host, port) | |
}); | |
function anyBodyParser(req, res, next) { | |
var data = ''; | |
req.setEncoding('utf8'); | |
req.on('data', function(chunk) { | |
data += chunk; | |
}); | |
req.on('end', function() { | |
req.rawBody = data; | |
next(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment