-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
65 lines (56 loc) · 2.02 KB
/
Copy pathproxy.js
File metadata and controls
65 lines (56 loc) · 2.02 KB
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
'use strict';
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
// To modify the proxy connection before data is sent, you can listen
// for the 'proxyReq' event. When the event is fired, you will receive
// the following arguments:
// (http.ClientRequest proxyReq, http.IncomingMessage req,
// http.ServerResponse res, Object options). This mechanism is useful when
// you need to modify the proxy request before the proxy connection
// is made to the target.
//
proxy.on('proxyReq', function(proxyReq, req, res, options) {
//console.log('in proxyReq:' + req.rawHeaders);
});
proxy.on('proxyRes', function(proxyRes, req, res, options) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Credentials', true);
});
// main front-server
var server = http.createServer(function(req, res) {
try {
if (req.method === 'OPTIONS') {
// preFlight request, proxy to mock server
proxy.web(req, res, {
target: 'http://localhost:8090'
});
return;
}
// api request, proxy to target backend-server
proxy.web(req, res, {
target: 'http://localhost:8080'
});
}catch(e) {
console.log(e);
}
});
//
// Create a mock server to handle preFlight request of CORS-protocol
//
http.createServer(function (req, res) {
res.writeHead(200,
{ 'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Credentials': true});
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 4));
res.end();
}).listen(8090);
console.log("listening on port 18080")
server.listen(18080);