diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-10-29 01:09:10 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-10-29 01:09:10 -0400 |
| commit | bd475ae6ae43cbe1d937ab7c4326ac0dda9bc81f (patch) | |
| tree | 175acdc988ec517e9bf2be73b699c695a237e366 /proxy | |
| parent | fffc000bd73b740318762747c07aa39adca1313a (diff) | |
make proxy work for gets
Diffstat (limited to 'proxy')
| -rw-r--r-- | proxy/index.js | 59 | ||||
| -rw-r--r-- | proxy/package.json | 5 |
2 files changed, 61 insertions, 3 deletions
diff --git a/proxy/index.js b/proxy/index.js index c2e12c60..159712e3 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -1,15 +1,70 @@ var http = require('http') +var najax = require('najax') var PORT = 4567 +var endpoint = "https://secure.api.yoox.biz" + var server = http.createServer(function (req, res){ - res.end('It Works!! Path Hit: ' + req.url) - console.log(req.headers) + console.log("_________________") + console.log("[200] " + req.method + " to " + req.url); + var headers = parse_headers(req) + console.log(headers) + + if (req.method != 'GET') { + var fullBody = ''; + + req.on('data', function(chunk) { + fullBody += chunk.toString(); + }); + + req.on('end', function() { + res.writeHead(200, "OK", {'Content-Type': 'text/html'}); + + // console.log(fullBody) + + najax({ + method: req.method, + url: endpoint + req.url, + data: fullBody, + headers: headers, + success: respond(res), + error: respond(res), + }) + }) + } + else { +// console.log(req) + najax({ + method: req.method, + url: endpoint + req.url, + headers: headers, + success: respond(res), + error: respond(res), + }) + } }) server.listen(PORT, function(){ console.log("Proxy listening on: http://localhost:%s", PORT) }) +function parse_headers (req) { + var headers = {} + var fields = "x-yoox-appname x-yoox-account-token x-yoox-device x-yoox-api-key x-yoox-cart-token".split(" ") + fields.forEach(function(field){ + if (req.headers[field]) headers[field] = field + }) + return headers +} + +function respond(res){ + return function(data, status, xhr){ + res.writeHead(xhr.status, "OK", { 'Content-Type': 'application/json' }) + res.end(data) + console.log(xhr.status, data.length) + } +} + /* headers: { "x-yoox-appname": auth.appname, diff --git a/proxy/package.json b/proxy/package.json index f7304d43..fd5c7ee2 100644 --- a/proxy/package.json +++ b/proxy/package.json @@ -7,5 +7,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "okfocus", - "license": "LNT" + "license": "LNT", + "dependencies": { + "najax": "^0.2.1" + } } |
