summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-29 10:36:01 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-29 10:36:01 +0200
commit4ed7271b1eca560fbad60c240cad23e85ac286f8 (patch)
tree0fc255912ec36735afe17c8870040d4ad768593c /routes
parentc04cb305f45e39b71a7bdd51f91e8076eae86481 (diff)
downloadcollection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.gz
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.bz2
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.xz
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.zip
o Adding examples from the specification.
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js57
1 files changed, 38 insertions, 19 deletions
diff --git a/routes/index.js b/routes/index.js
index 1a05e39..0bd432d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -3,34 +3,53 @@ var http = require('http')
, collection_json = require('collection_json');
exports.index = function(req, res){
- res.render('index', { title: 'Express' });
+ res.render('index', {
+ title: 'Collection+JSON Explorer',
+ host: req.headers.host,
+ examples: [
+ "minimal",
+ "collection",
+ "item",
+ "queries",
+ "template",
+ "error"
+ ]
+ });
};
exports.render = function(req, res){
- var options = url.parse(req.query.url);
- options.method = 'GET';
- options.headers = {
- accept: 'application/vnd.collection+json'
- };
- var clientReq = http.request(options, function(clientRes) {
- clientRes.setEncoding('utf8');
- var body = '';
- clientRes.on('data', function (chunk) {
- body += chunk;
- });
- clientRes.on('end', function (chunk) {
+ fetchCollection(req.query.url, function(err, headers, body) {
+ if(err) {
+ res.render('index', { title: 'Failed to render ' + req.query.url });
+ }
+ else {
var parsedBody = JSON.parse(body);
var doc = collection_json.fromObject(parsedBody);
res.render('data', {
url: req.query.url,
doc: doc,
- headers: clientRes.headers,
+ headers: headers,
raw: body,
formattedRaw: JSON.stringify(parsedBody, null, ' ') });
- });
- });
- clientReq.on('error', function() {
- res.render('index', { title: 'Failed to render ' + req.query.url });
+ }
});
- clientReq.end();
};
+
+function fetchCollection(u, cb) {
+ var options = url.parse(u);
+ options.headers = {
+ accept: 'application/vnd.collection+json'
+ };
+ var req = http.get(options, function(res) {
+ res.setEncoding('utf8');
+ var body = '';
+ res.on('data', function (chunk) {
+ body += chunk;
+ });
+ res.on('end', function (chunk) {
+ cb(undefined, res.headers, body);
+ });
+ }).on('error', function() {
+ cb('Unable to fetch ' + u);
+ });
+}