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){ 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, "x-yoox-account-token": auth.access_token, "x-yoox-device": auth.device, "x-yoox-api-key": auth.apikey, "x-yoox-cart-token": cart.token, }, proxy content body proxy method proxy query string */