summaryrefslogtreecommitdiff
path: root/proxy/index.js
blob: 159712e3c75f4eac0465de3b72048803cfc4c2e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
*/