Skip to content

Commit e02317c

Browse files
committed
[examples] updated old proxy examples
1 parent b726116 commit e02317c

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

Diff for: examples/http/proxy-https-to-http.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
proxy-https-to-http.js: Basic example of proxying over HTTPS to a target HTTP server
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var https = require('https'),
28+
http = require('http'),
29+
util = require('util'),
30+
path = require('path'),
31+
fs = require('fs'),
32+
colors = require('colors'),
33+
httpProxy = require('../../lib/http-proxy'),
34+
fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures');
35+
36+
//
37+
// Create the target HTTP server
38+
//
39+
http.createServer(function (req, res) {
40+
res.writeHead(200, { 'Content-Type': 'text/plain' });
41+
res.write('hello http over https\n');
42+
res.end();
43+
}).listen(9000);
44+
45+
//
46+
// Create the HTTPS proxy server listening on port 8000
47+
//
48+
httpProxy.createServer({
49+
target: {
50+
host: 'localhost',
51+
port: 9000
52+
},
53+
ssl: {
54+
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
55+
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
56+
}
57+
}).listen(8000);
58+
59+
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
60+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

Diff for: examples/http/proxy-https-to-https.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
proxy-https-to-https.js: Basic example of proxying over HTTPS to a target HTTPS server
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var https = require('https'),
28+
http = require('http'),
29+
util = require('util'),
30+
fs = require('fs'),
31+
path = require('path'),
32+
colors = require('colors'),
33+
httpProxy = require('../../lib/http-proxy'),
34+
fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures'),
35+
httpsOpts = {
36+
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
37+
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
38+
};
39+
40+
//
41+
// Create the target HTTPS server
42+
//
43+
https.createServer(httpsOpts, function (req, res) {
44+
res.writeHead(200, { 'Content-Type': 'text/plain' });
45+
res.write('hello https\n');
46+
res.end();
47+
}).listen(9000);
48+
49+
//
50+
// Create the proxy server listening on port 443
51+
//
52+
httpProxy.createServer({
53+
ssl: httpsOpts,
54+
target: 'https://localhost:9000',
55+
secure: false
56+
}).listen(8000);
57+
58+
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
59+
util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

0 commit comments

Comments
 (0)