mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[doc] Updated examples
This commit is contained in:
parent
2cd8256c4d
commit
13eaec55dc
@ -1,5 +1,4 @@
|
|||||||
|
|
||||||
module.exports = Store
|
|
||||||
//
|
//
|
||||||
// just to make these example a little bit interesting,
|
// just to make these example a little bit interesting,
|
||||||
// make a little key value store with an http interface
|
// make a little key value store with an http interface
|
||||||
@ -20,12 +19,10 @@ module.exports = Store
|
|||||||
//
|
//
|
||||||
// TODO: cached map-reduce views and auto-magic sharding.
|
// TODO: cached map-reduce views and auto-magic sharding.
|
||||||
//
|
//
|
||||||
|
var Store = module.exports = function Store () {
|
||||||
|
this.store = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Store () {
|
|
||||||
this.store = {}
|
|
||||||
}
|
|
||||||
Store.prototype = {
|
Store.prototype = {
|
||||||
get: function (key) {
|
get: function (key) {
|
||||||
return this.store[key]
|
return this.store[key]
|
||||||
|
|||||||
@ -27,16 +27,17 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
|
var welcome = [
|
||||||
|
'# # ##### ##### ##### ##### ##### #### # # # #',
|
||||||
|
'# # # # # # # # # # # # # # # # ',
|
||||||
|
'###### # # # # ##### # # # # # # ## # ',
|
||||||
|
'# # # # ##### ##### ##### # # ## # ',
|
||||||
|
'# # # # # # # # # # # # # ',
|
||||||
|
'# # # # # # # # #### # # # '
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
// ascii art from http://github.com/marak/asciimo
|
|
||||||
var welcome = '\
|
|
||||||
# # ##### ##### ##### ##### ##### #### # # # # \n\
|
|
||||||
# # # # # # # # # # # # # # # # \n\
|
|
||||||
###### # # # # ##### # # # # # # ## # \n\
|
|
||||||
# # # # ##### ##### ##### # # ## # \n\
|
|
||||||
# # # # # # # # # # # # # \n\
|
|
||||||
# # # # # # # # #### # # # \n';
|
|
||||||
util.puts(welcome.rainbow.bold);
|
util.puts(welcome.rainbow.bold);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Basic Http Proxy Server
|
// Basic Http Proxy Server
|
||||||
@ -42,23 +42,24 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
|
|||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
var connections = []
|
var connections = [],
|
||||||
, go
|
go;
|
||||||
|
|
||||||
http.createServer(function (req, res) {
|
http.createServer(function (req, res) {
|
||||||
|
connections.push(function () {
|
||||||
connections.push (function (){
|
|
||||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||||
res.end();
|
res.end();
|
||||||
})
|
});
|
||||||
process.stdout.write(connections.length + ', ')
|
|
||||||
if (connections.length > 110 || go) {
|
|
||||||
go = true
|
|
||||||
while(connections.length)
|
|
||||||
connections.shift()()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
process.stdout.write(connections.length + ', ');
|
||||||
|
|
||||||
|
if (connections.length > 110 || go) {
|
||||||
|
go = true;
|
||||||
|
while (connections.length) {
|
||||||
|
connections.shift()();
|
||||||
|
}
|
||||||
|
}
|
||||||
}).listen(9000);
|
}).listen(9000);
|
||||||
|
|
||||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
||||||
|
|||||||
@ -27,17 +27,12 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Http Proxy Server with Latency
|
// Http Proxy Server with Latency
|
||||||
//
|
//
|
||||||
var server = httpProxy.createServer(function (req, res, proxy) {
|
var server = httpProxy.createServer(9000, 'localhost');
|
||||||
proxy.proxyRequest(req, res, {
|
|
||||||
port: 9000,
|
|
||||||
host: 'localhost'
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Tell the server to listen on port 8002
|
// Tell the server to listen on port 8002
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Setup proxy server with forwarding
|
// Setup proxy server with forwarding
|
||||||
|
|||||||
@ -27,13 +27,13 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Http Proxy Server with Latency
|
// Http Proxy Server with Latency
|
||||||
//
|
//
|
||||||
httpProxy.createServer(function (req, res, proxy) {
|
httpProxy.createServer(function (req, res, proxy) {
|
||||||
var buffer = proxy.buffer(req);
|
var buffer = httpProxy.buffer(req);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
proxy.proxyRequest(req, res, {
|
proxy.proxyRequest(req, res, {
|
||||||
port: 9000,
|
port: 9000,
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var https = require('https'),
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
httpProxy = require('./../lib/node-http-proxy'),
|
httpProxy = require('../../lib/node-http-proxy'),
|
||||||
helpers = require('./../test/helpers');
|
helpers = require('./../test/helpers');
|
||||||
|
|
||||||
var opts = helpers.loadHttps();
|
var opts = helpers.loadHttps();
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var https = require('https'),
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
httpProxy = require('./../lib/node-http-proxy'),
|
httpProxy = require('../../lib/node-http-proxy'),
|
||||||
helpers = require('./../test/helpers');
|
helpers = require('./../test/helpers');
|
||||||
|
|
||||||
var opts = helpers.loadHttps();
|
var opts = helpers.loadHttps();
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Http Proxy Server with Proxy Table
|
// Http Proxy Server with Proxy Table
|
||||||
|
|||||||
@ -27,14 +27,14 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Http Server with proxyRequest Handler and Latency
|
// Http Server with proxyRequest Handler and Latency
|
||||||
//
|
//
|
||||||
var proxy = new httpProxy.HttpProxy();
|
var proxy = new httpProxy.HttpProxy();
|
||||||
http.createServer(function (req, res) {
|
http.createServer(function (req, res) {
|
||||||
var buffer = proxy.buffer(req);
|
var buffer = httpProxy.buffer(req);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
proxy.proxyRequest(req, res, {
|
proxy.proxyRequest(req, res, {
|
||||||
port: 9000,
|
port: 9000,
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
http = require('http'),
|
http = require('http'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Basic Http Proxy Server
|
// Basic Http Proxy Server
|
||||||
|
|||||||
@ -37,7 +37,7 @@ httpProxy.createServer(
|
|||||||
// This is where our middlewares go, with any options desired - in this case,
|
// This is where our middlewares go, with any options desired - in this case,
|
||||||
// the list of routes/URLs and their destinations.
|
// the list of routes/URLs and their destinations.
|
||||||
//
|
//
|
||||||
require('proxy-by-url')({
|
require('proxy-by-url')({
|
||||||
'/hello': { port: 9000, host: 'localhost' },
|
'/hello': { port: 9000, host: 'localhost' },
|
||||||
'/charlie': { port: 80, host: 'charlieistheman.com' },
|
'/charlie': { port: 80, host: 'charlieistheman.com' },
|
||||||
'/google': { port: 80, host: 'google.com' }
|
'/google': { port: 80, host: 'google.com' }
|
||||||
|
|||||||
@ -19,12 +19,11 @@ httpProxy.createServer(
|
|||||||
//
|
//
|
||||||
// Target Http Server (to listen for requests on 'localhost')
|
// Target Http Server (to listen for requests on 'localhost')
|
||||||
//
|
//
|
||||||
http.createServer(
|
http.createServer(function (req, res) {
|
||||||
function (req, res) {
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
res.end();
|
||||||
res.end();
|
}).listen(9000);
|
||||||
}).listen(9000);
|
|
||||||
|
|
||||||
// And finally, some colored startup output.
|
// And finally, some colored startup output.
|
||||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var sys = require('sys'),
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
websocket = require('./../vendor/websocket'),
|
websocket = require('./../vendor/websocket'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var utils = require('socket.io/lib/socket.io/utils'),
|
var utils = require('socket.io/lib/socket.io/utils'),
|
||||||
@ -80,7 +80,7 @@ var proxyServer = http.createServer(function (req, res) {
|
|||||||
// WebSocket requests as well.
|
// WebSocket requests as well.
|
||||||
//
|
//
|
||||||
proxyServer.on('upgrade', function (req, socket, head) {
|
proxyServer.on('upgrade', function (req, socket, head) {
|
||||||
var buffer = proxy.buffer(socket);
|
var buffer = httpProxy.buffer(socket);
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
proxy.proxyWebSocketRequest(req, socket, head, {
|
proxy.proxyWebSocketRequest(req, socket, head, {
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var sys = require('sys'),
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
websocket = require('./../vendor/websocket'),
|
websocket = require('./../vendor/websocket'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var utils = require('socket.io/lib/socket.io/utils'),
|
var utils = require('socket.io/lib/socket.io/utils'),
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var sys = require('sys'),
|
|||||||
http = require('http'),
|
http = require('http'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
websocket = require('./../vendor/websocket'),
|
websocket = require('./../vendor/websocket'),
|
||||||
httpProxy = require('./../lib/node-http-proxy');
|
httpProxy = require('../../lib/node-http-proxy');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var utils = require('socket.io/lib/socket.io/utils'),
|
var utils = require('socket.io/lib/socket.io/utils'),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user