From 9c933e4eb578b77d625efe487866e7433d729666 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 28 Jun 2012 23:22:57 +0200 Subject: o Initial import of application/vnd.collection+json explorer. --- routes/index.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 routes/index.js (limited to 'routes') diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..9bd03cb --- /dev/null +++ b/routes/index.js @@ -0,0 +1,37 @@ +var http = require('http') + , url = require('url') + , collection_json = require('collection_json'); + +exports.index = function(req, res){ + res.render('index', { title: 'Express' }); +}; + +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) { + console.log('STATUS: ' + clientRes.statusCode); + console.log('HEADERS: ' + JSON.stringify(clientRes.headers)); + clientRes.setEncoding('utf8'); + var body = ""; + clientRes.on('data', function (chunk) { + body += chunk; + }); + clientRes.on('end', function (chunk) { + var doc = collection_json.fromObject(JSON.parse(body)); + res.render('data', { + title: 'Rendering ' + req.query.url, + doc: doc, + headers: clientRes.headers, + raw: body, + formattedRaw: JSON.stringify(JSON.parse(body), null, ' ') }); + }); + }); + clientReq.on('error', function() { + res.render('index', { title: 'Failed to render ' + req.query.url }); + }); + clientReq.end(); +}; -- cgit v1.2.3