Skip to content

Commit 4490c0b

Browse files
committed
Add command line arguments analagous to the old dev server
1 parent a0c2382 commit 4490c0b

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"jsdoc": "3.3.0-alpha8",
1313
"express": "4.4.4",
1414
"compression": "1.0.8",
15-
"request": "2.36.0"
15+
"request": "2.36.0",
16+
"yargs": "1.2.6"
1617
}
1718
}

server.js

+44-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@
88
var url = require('url');
99
var request = require('request');
1010

11+
var yargs = require('yargs').options({
12+
'port' : {
13+
'default' : 8080,
14+
'description' : 'Port to listen on.'
15+
},
16+
'public' : {
17+
'type' : 'boolean',
18+
'description' : 'Run a public server that listens on all interfaces.'
19+
},
20+
'upstream-proxy' : {
21+
'description' : 'A standard proxy server that will be used to retrieve data. Specify a URL including port, e.g. "http://proxy:8000".'
22+
},
23+
'bypass-upstream-proxy-hosts' : {
24+
'description' : 'A comma separated list of hosts that will bypass the specified upstream_proxy, e.g. "lanhost1,lanhost2"'
25+
},
26+
'help' : {
27+
'alias' : 'h',
28+
'type' : 'boolean',
29+
'description' : 'Show this help.'
30+
}
31+
});
32+
var argv = yargs.argv;
33+
34+
if (argv.help) {
35+
return yargs.showHelp();
36+
}
37+
1138
// eventually this mime type configuration will need to change
1239
// https://github.com/visionmedia/send/commit/d2cb54658ce65948b0ed6e5fb5de69d022bef941
1340
var mime = express.static.mime;
@@ -47,6 +74,14 @@
4774
return result;
4875
}
4976

77+
var upstreamProxy = argv['upstream-proxy'];
78+
var bypassUpstreamProxyHosts = {};
79+
if (argv['bypass-upstream-proxy-hosts']) {
80+
argv['bypass-upstream-proxy-hosts'].split(',').forEach(function(host) {
81+
bypassUpstreamProxyHosts[host.toLowerCase()] = true;
82+
});
83+
}
84+
5085
app.get('/proxy/*', function(req, res, next) {
5186
// look for request like http://localhost:8080/proxy/http://example.com/file?query=1
5287
var remoteUrl = getRemoteUrlFromParam(req);
@@ -66,14 +101,18 @@
66101
remoteUrl.protocol = 'http:';
67102
}
68103

69-
remoteUrl = url.format(remoteUrl);
104+
var proxy;
105+
if (upstreamProxy && !(remoteUrl.host in bypassUpstreamProxyHosts)) {
106+
proxy = upstreamProxy;
107+
}
70108

71109
// encoding : null means "body" passed to the callback will be raw bytes
72110

73111
request.get({
74-
url : remoteUrl,
112+
url : url.format(remoteUrl),
75113
headers : filterHeaders(req, req.headers),
76-
encoding : null
114+
encoding : null,
115+
proxy : proxy
77116
}, function(error, response, body) {
78117
var code = 500;
79118

@@ -86,7 +125,7 @@
86125
});
87126
});
88127

89-
app.listen(8080);
128+
app.listen(argv.port, argv.public ? undefined : 'localhost');
90129

91-
console.log('Cesium development server running. Connect to http://localhost:8080.');
130+
console.log('Cesium development server running. Connect to http://localhost:%d.', argv.port);
92131
})();

0 commit comments

Comments
 (0)