diff options
Diffstat (limited to 'routes')
-rw-r--r-- | routes/index.js | 37 |
1 files changed, 37 insertions, 0 deletions
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(); +}; |