diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-14 00:56:14 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-07-14 00:56:14 +0200 |
commit | d549928c12e79bae8069fe5fe4e067ad0c608101 (patch) | |
tree | bedc08c6b9ae4087e7be11d61c144234a963ab20 /routes | |
parent | 15800d991e50d15b085d2953c2e1af68166131f5 (diff) | |
download | collection-json-explorer-d549928c12e79bae8069fe5fe4e067ad0c608101.tar.gz collection-json-explorer-d549928c12e79bae8069fe5fe4e067ad0c608101.tar.bz2 collection-json-explorer-d549928c12e79bae8069fe5fe4e067ad0c608101.tar.xz collection-json-explorer-d549928c12e79bae8069fe5fe4e067ad0c608101.zip |
o Only showing headings if the item is in the colleciton.
o Adding full support for writes.
o Better looking query and template forms.
Diffstat (limited to 'routes')
-rw-r--r-- | routes/index.js | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/routes/index.js b/routes/index.js index cace65e..1edd536 100644 --- a/routes/index.js +++ b/routes/index.js @@ -29,6 +29,16 @@ function split(str) { } exports.split = split; +function extractParams(o) { + return _.reduce(o, function(q, value, key) { + if(!key.match(/^param-/)) { + return q; + } + q[key.substr(6)] = value; + return q; + }, {}); +} + function urlgenerator(req) { var host = req.headers.host; return { @@ -90,15 +100,50 @@ exports.delete = function(req, res) { }).end(); } +exports.write = function(req, res) { + var options = url.parse(req.body.url, false); + options.method = 'POST'; + options.headers = { + 'Content-Type': 'application/vnd.collection+json' + }; +// console.log('options', options); + +// console.log('params', extractParams(req.body)); + var data = _.map(extractParams(req.body), function(value, key) { + return {name: key, value: value}; + }); + var body = {template: { data: data }}; +// console.log('body', JSON.stringify(body)); + function done(message, httpResponse) { + res.render('data', { + urlgenerator: urlgenerator(req), + url: req.body.url, + httpResponse: httpResponse + }); + } + var httpRequest = http.request(options, function(httpResponse) { + httpResponse.setEncoding('utf8'); + var body = ''; + httpResponse.on('data', function (chunk) { + body += chunk; + }).on('end', function (chunk) { + done(undefined, { + statusCode: httpResponse.statusCode, + status: '', + headers: httpResponse.headers, + body: body + }); + }); + }).on('error', function(e) { + done(util.inspect(e)); + }); + httpRequest.write(JSON.stringify(body)); + httpRequest.end(); +} + exports.render = function(req, res) { var u = url.parse(req.query.url, true); - var params = _.reduce(req.query, function(q, value, key) { - if(!key.match(/^param-/)) { - return q; - } - q[key.substr(6)] = value; - return q; - }, {}); + var params = extractParams(req.query); u.query = _.extend({}, u.query, params); fetchCollection(url.format(u), function(err, httpResponse) { if(err) { |