-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (50 loc) · 1.49 KB
/
index.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
var url = require('url');
var http = require('http');
var httpProxy = require('http-proxy');
var port = process.env.VIRTUAL_PORT || 8080;
var api = {
env: process.env.NTB_ENV || 'api',
key: process.env.NTB_KEY
};
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
//
// 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 = http.createServer(function(req, res) {
// Log requests
console.log(new Date(), req.method, req.url);
// Add CORS Headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'HEAD, GET, OPTIONS');
res.setHeader('Access-Control-Expose-Headers', [
'Count-Return',
'Count-Total',
'ETag',
'Last-Modified',
'Location',
'X-Cache-Hit',
'X-Ratelimit-Limit',
'X-Ratelimit-Remaining',
'X-Ratelimit-Reset'
].join(' '));
// COORS preflight
if (req.method === 'OPTIONS') {
res.statusCode = 204;
res.end();
}
// Add HTTP Header
req.headers.host = api.env + '.nasjonalturbase.no'
// Add API key parameter
u = url.parse(req.url, true);
u.query.api_key = api.key;
u.search = undefined; // query will only be used if search is absent
req.url = url.format(u);
proxy.web(req, res, { target: 'http://' + req.headers.host });
});
console.log('listening on port ' + port)
server.listen(port);