-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathnode-http-proxy
executable file
·76 lines (66 loc) · 1.8 KB
/
node-http-proxy
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
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
util = require('util'),
argv = require('optimist').argv,
httpProxy = require('./../lib/node-http-proxy');
var help = [
"usage: node-http-proxy [options] ",
"",
"All options should be set with the syntax --option=value",
"",
"options:",
" --port PORT Port that the proxy server should run on",
" --target HOST:PORT Location of the server the proxy will target",
" --config OUTFILE Location of the configuration file for the proxy server",
" --silent Silence the log output from the proxy server",
" -h, --help You're staring at it"
];
if (argv.h || argv.help || Object.keys(argv).length === 2) {
util.puts(help.join('\n'));
process.exit(0);
}
var location, config = {},
port = argv.port || 80,
target = argv.target;
//
// If we were passed a config, parse it
//
if (argv.config) {
try {
var data = fs.readFileSync(argv.config);
config = JSON.parse(data.toString());
} catch (ex) {
util.puts('Error starting node-http-proxy: ' + ex);
process.exit(1);
}
}
//
// Check to see if we should silence the logs
//
config.silent = typeof argv.silent !== 'undefined' ? argv.silent : config.silent;
//
// If we were passed a target, parse the url string
//
if (target) location = target.split(':');
//
// Create the server with the specified options
//
var server;
if (location) {
var targetPort = location.length === 1 ? 80 : location[1];
server = httpProxy.createServer(targetPort, location[0], config);
}
else {
server = httpProxy.createServer(config);
}
//
// Start the server
//
server.listen(port);
//
// Notify that the server is started
//
if (!config.silent) {
util.puts('node-http-proxy server now listening on port: ' + port);
}