| 37 | }; |
| 38 | |
| 39 | var proxy = function(req, res, next) { |
| 40 | try { |
| 41 | var options = mapRequest(req); |
| 42 | // Create the request to the db |
| 43 | var dbReq = https.request(options, function(dbRes) { |
| 44 | var data = ""; |
| 45 | res.headers = dbRes.headers; |
| 46 | dbRes.setEncoding('utf8'); |
| 47 | dbRes.on('data', function(chunk) { |
| 48 | // Pass back any data from the response. |
| 49 | data = data + chunk; |
| 50 | }); |
| 51 | dbRes.on('end', function() { |
| 52 | res.header('Content-Type', 'application/json'); |
| 53 | res.statusCode = dbRes.statusCode; |
| 54 | res.httpVersion = dbRes.httpVersion; |
| 55 | res.trailers = dbRes.trailers; |
| 56 | res.send(data); |
| 57 | res.end(); |
| 58 | }); |
| 59 | }); |
| 60 | // Send any data the is passed from the original request |
| 61 | dbReq.end(JSON.stringify(req.body)); |
| 62 | } catch (error) { |
| 63 | console.log('ERROR: ', error.stack); |
| 64 | res.json(error); |
| 65 | res.end(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | // Attach the mapurl fn (mostly for testing) |
| 70 | proxy.mapUrl = mapUrl; |