diff options
Diffstat (limited to 'proxy/index.js')
| -rw-r--r-- | proxy/index.js | 59 |
1 files changed, 57 insertions, 2 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, |
