Permalink
Browse files

adding lib

  • Loading branch information...
1 parent 063c4bb commit 87f16d1bc1a9002c3d7c088eed9aabdc6928da77 @joshbirk committed Feb 3, 2012
Showing with 58 additions and 0 deletions.
  1. +58 −0 lib/lazy-proxy/index.js
@@ -0,0 +1,58 @@
+var https = require('https'); //need to offer switch
+var http = require('http');
+
+
+function send(options, req, res){
+ var label = req.params.label;
+ var mode = req.params.mode;
+ var host = options.host;
+ var http = http;
+ if(options.useHTTPS) {
+ http = https;
+ }
+
+ var data = '';
+
+ //define endpoint
+ var path = req.url.replace('/'+label+'/'+mode,'');
+ var page = null;
+
+ if(mode == 'view') {
+ page = path.split('/')[1];
+ path = path.replace('/'+page,'');
+ }
+
+ var proxy_options = {
+ host: options.host,
+ path: path,
+ method: req.method,
+ headers: options.headers
+ };
+ console.log(proxy_options);
+
+ var _req = http.request(proxy_options, function(_res) {
+ _res.on('data', function(_data) {
+ data += _data;
+ });
+
+ _res.on('end', function(d) {
+ console.log("DATA");
+ if(mode == "json") {
+ res.send(data);
+ }
+ if(mode == "view") {
+ res.render(page,JSON.parse( data ));
+ }
+ });
+
+ }).on('error', function(e) {
+ console.log("ERROR");
+ console.log(e);
+ res.send(e);
+ });
+
+ _req.end();
+ }
+
+
+exports.send = send;

0 comments on commit 87f16d1

Please sign in to comment.