-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy.js
45 lines (38 loc) · 971 Bytes
/
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
/* jshint: node */
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
const proxyServer = function(req) {
if (req.headers.accept) {
return req.headers.accept.indexOf('application/json') > -1;
}
};
const assetServer = function(req) {
return req.url.startsWith('/js/') ||
req.url.startsWith('/css/') ||
req.url.startsWith('/fonts/') ||
req.url.startsWith('/img/');
};
const server = http.createServer(function(req, res) {
if (proxyServer(req)) {
console.log('json');
proxy.web(req, res, {
target: 'http://localhost:3000'
});
}
else if (assetServer(req)) {
console.log('assets');
proxy.web(req, res, {
target: 'http://localhost:8080'
});
}
else {
console.log('index');
req.url = '/';
proxy.web(req, res, {
target: 'http://localhost:8080'
});
}
});
console.log('Listening on port 8000');
server.listen(8000);