From 4ed7271b1eca560fbad60c240cad23e85ac286f8 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 29 Jun 2012 10:36:01 +0200 Subject: o Adding examples from the specification. --- routes/index.js | 57 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'routes') 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); + }); +} -- cgit v1.2.3