Skip to content

Instantly share code, notes, and snippets.

@milovanderlinden
Last active October 28, 2015 23:18
Show Gist options
  • Save milovanderlinden/1893942c39cdcd004b92 to your computer and use it in GitHub Desktop.
Save milovanderlinden/1893942c39cdcd004b92 to your computer and use it in GitHub Desktop.
Want to parse a OGC GetCapabilities Document to JSON with nodejs?. This will bring filesize down to 10%!
var allOGC = function(req, res) {
if (req.query) {
//set method from req
console.log(req.method);
var options = {
method: req.method,
url: req.query.q,
headers: {
'User-Agent': 'opendispatcher'
},
timeout: 6000 //6 seconds
};
var request = require('request');
x = request(options);
x.on('response', function(response) {
var data = [];
response.on('data', function(chunk) {
data.push(chunk);
});
response.on('end', function() {
var finaldata = data.join('');
var xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray: false, mergeAttrs: true, stripPrefix: true});
parser.parseString(finaldata, function(err, result) {
if (err) {
console.log(err);
res.end();
} else {
res.json(result);
}
});
});
});
x.on('error', function(err) {
res.status(400).json({
"error": "Timeout on proxy"
});
});
} else {
res.status(400).json({
"error": "wrong use of proxy"
});
}
};
router.route('/ogc/').all(allOGC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment