This repository was archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.js
34 lines (30 loc) · 1.38 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
var express = require('express');
var proxy = require('http-proxy-middleware');
var app = express();
var proxyOptions = {
onProxyRes: function(proxyRes, req, res) {
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS';
proxyRes.headers['Access-Control-Allow-Headers'] = 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
},
onProxyReq: function(proxyReq, req, res) {
proxyReq.path = req.originalUrl.substring(`/api/${req.params.region}`.length);
},
router: function(req) {
const targetPath = req.originalUrl.substring(`/api/${req.params.region}`.length);
return `https://api.${req.params.region}.bluemix.net${targetPath}`;
},
target: 'dynamic',
changeOrigin: true,
logLevel: 'debug'
};
app.options('/api/:region/*', (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.set('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization');
res.send('OK');
});
app.use('/api/:region', proxy(proxyOptions));
app.listen(process.env.PORT || 8080, function() {
console.log('server listening on', process.env.PORT || 8080);
});