forked from forcedotcom/mobile-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
63 lines (51 loc) · 1.9 KB
/
proxy.js
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
var http = require('http'),
httpProxy = require('http-proxy'),
url = require('url'),
express = require('express');
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({ secure: true });
//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = require('http').createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
var endpoint = req.headers['salesforceproxy-endpoint'];
var target = 'http://localhost:8000';
if (endpoint) {
var auth = req.headers['x-authorization'];
if (auth) req.headers['Authorization'] = auth;
// http-proxy module uses the url path to generate the path of new outgoing request
req.url = endpoint;
// Building the target host url from the endpoint information.
var targetURL = url.parse(endpoint);
target = targetURL.protocol + "//" + targetURL.host;
}
proxy.web(req, res, { target: target });
});
//
// Listen for the `error` event on `proxy`.
server.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
//
// Listen for the `proxyRes` event on `proxy`.
//
server.on('proxyRes', function (res) {
console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});
server.listen(process.env.PORT || 9000);
console.log('Listening on port %d', server.address().port);
/***** SETUP EXPRESS SERVER FOR DESIGNER ****/
// Create express application (http://expressjs.com) ###
var app = express();
app.use(express.static(__dirname));
var appServer = app.listen(8000, function() {
console.log('Listening on port %d', appServer.address().port);
});