summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-30 01:18:33 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-30 01:18:33 +0200
commit4af799c68089d2a56f69fc67c4c0cd9c82d176b8 (patch)
tree3aa05e2caae87e5c8e0b46901577fd19039565ec /routes
parentcc70ec9cb09172167e7ed334b2ab1def391c6d59 (diff)
downloadcollection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.gz
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.bz2
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.xz
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.zip
o Prettier and better error handling.
Diffstat (limited to 'routes')
-rw-r--r--routes/index.js26
1 files changed, 22 insertions, 4 deletions
diff --git a/routes/index.js b/routes/index.js
index 6d57023..bceafc3 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -2,6 +2,14 @@ var http = require('http')
, url = require('url')
, collection_json = require('collection_json');
+function urlgenerator(req) {
+ var host = req.headers.host;
+ return {
+ render: function(u) {
+ return 'http://' + host + '/render?url=' + encodeURIComponent(u)}
+ };
+}
+
exports.index = function(req, res){
res.render('index', {
title: 'Collection+JSON Explorer',
@@ -17,14 +25,23 @@ exports.index = function(req, res){
});
};
-exports.render = function(req, res){
+exports.render = function(req, res) {
+ function sendErr(err) {
+ res.render('data', {
+ url: req.query.url,
+ err: err
+ });
+ }
fetchCollection(req.query.url, function(err, headers, body) {
if(err) {
- res.render('index', { title: 'Failed to render ' + req.query.url });
+ sendErr(err);
}
else {
- var parsedBody = JSON.parse(body);
- var doc = collection_json.fromObject(parsedBody);
+ var parsedBody;
+ try {
+ parsedBody = JSON.parse(body);
+ } catch(e) { sendErr('Unable to parse JSON: ' + e); }
+ var doc = collection_json.fromObject(parsedBody).collection;
var isUrl = function(u) {
try {
var x = url.parse(u);
@@ -36,6 +53,7 @@ exports.render = function(req, res){
};
res.render('data', {
isUrl: isUrl,
+ urlgenerator: urlgenerator(req),
url: req.query.url,
doc: doc,
headers: headers,