-
Notifications
You must be signed in to change notification settings - Fork 7
/
static_server.js
85 lines (74 loc) · 2.83 KB
/
static_server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var http = require("http"),
url = require("url"),
httpProxy = require('http-proxy'),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 8888,
////////////////////////////////////////////////////////////////////////////
// Adjust this settings to your needs for proxying the backend requests //
////////////////////////////////////////////////////////////////////////////
proxy_cfg = {
// the prefix you use to call your backend functions via the proxy server
prefix: "/proxy/",
// the host of your backend server
host: "services.odata.org",
// port of your backend server
port: ""
};
var proxy = httpProxy.createProxyServer();
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
if (uri.indexOf(proxy_cfg.prefix) === 0) {
proxy.on('error', function (err, req, res) {
//console.log("backend error");
//console.log(err);
});
proxy.on('proxyRes', function (proxyRes, req, res) {
//console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
proxy.on('close', function (req, socket, head) {
// view disconnected websocket connections
//console.log('Client disconnected');
});
// We have to set the host of the request to the our remote server
// It currently contains localhost... which leads to problems on some
// servers
request.headers.host = proxy_cfg.host;
// cut the prefix from the beginning of the url
// request.url = request.url.slice(request.url.indexOf("/", 1));
request.url = request.url.slice(proxy_cfg.prefix.length);
proxy.web(request, response, {
// cause we use this script only during development and testing we
// have a http connection. For https we have to do some additional
// proxy configuration
target: 'http://' + proxy_cfg.host + (proxy_cfg.port ? ':' + proxy_cfg.port : '') + '/'
});
} else {
fs.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {
"Content-Type": "text/plain"
});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if (err) {
response.writeHead(500, {
"Content-Type": "text/plain"
});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}
}).listen(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");