Name: node-http-proxy
Owner: Digital Bazaar, Inc.
Description: A full-featured http proxy for node.js
Created: 2014-06-29 02:09:14.0
Updated: 2014-07-31 18:00:27.0
Pushed: 2014-07-18 15:46:30.0
Homepage: http://github.com/nodejitsu/node-http-proxy
Size: 1598
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
node-http-proxy
is an HTTP programmable proxying library that supports
websockets. It is suitable for implementing components such as
proxies and load balancers.
A new proxy is created by calling createProxyServer
and passing
an options
object as argument (valid properties are available here)
httpProxy = require('http-proxy');
proxy = httpProxy.createProxyServer(options);
An object will be returned with four values:
req, res, [options]
(used for proxying regular HTTP(S) requests)req, socket, head, [options]
(used for proxying WS(S) requests)port
(a function that wraps the object in a webserver, for your convenience)Is it then possible to proxy requests by calling these functions
ire('http').createServer(function(req, res) {
oxy.web(req, res, { target: 'http://mytarget.com:8080' });
Errors can be listened on either using the Event Emitter API
y.on('error', function(e) {
.
or using the callback API
y.web(req, res, { target: 'http://mytarget.com:8080' }, function(e) { ... });
When a request is proxied it follows two different pipelines (available here)
which apply transformations to both the req
and res
object.
The first pipeline (ingoing) is responsible for the creation and manipulation of the stream that connects your client to the target.
The second pipeline (outgoing) is responsible for the creation and manipulation of the stream that, from your target, returns data
to the client.
http = require('http'),
httpProxy = require('http-proxy');
reate your proxy server and set the target in the options.
Proxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);
reate your target server
.createServer(function (req, res) {
s.writeHead(200, { 'Content-Type': 'text/plain' });
s.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
s.end();
isten(9000);
This example show how you can proxy a request using your own HTTP server and also you can put your own logic to handle the request.
http = require('http'),
httpProxy = require('http-proxy');
reate a proxy server with custom application logic
proxy = httpProxy.createProxyServer({});
reate your custom server and just call `proxy.web()` to proxy
web request to the target passed in the options
lso you can use `proxy.ws()` to proxy a websockets request
server = require('http').createServer(function(req, res) {
You can define here your custom logic to handle the request
and then proxy the request.
oxy.web(req, res, { target: 'http://127.0.0.1:5060' });
ole.log("listening on port 5050")
er.listen(5050);
http = require('http'),
httpProxy = require('http-proxy');
reate a proxy server with latency
proxy = httpProxy.createProxyServer();
reate your server that make an operation that take a while
nd then proxy de request
.createServer(function (req, res) {
This simulate an operation that take 500ms in execute
tTimeout(function () {
proxy.web(req, res, {
target: 'http://localhost:9008'
});
500);
isten(8008);
reate your target server
.createServer(function (req, res) {
s.writeHead(200, { 'Content-Type': 'text/plain' });
s.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
s.end();
isten(9008);
error
: The error event is emitted if the request to the target fail.proxyRes
: This event is emitted if the request to the target got a response.httpProxy = require('http-proxy');
rror example
ttp Proxy Server with bad target
proxy = httpProxy.createServer({
rget:'http://localhost:9005'
y.listen(8005);
isten for the `error` event on `proxy`.
y.on('error', function (err, req, res) {
s.writeHead(500, {
'Content-Type': 'text/plain'
;
s.end('Something went wrong. And we are reporting a custom error message.');
isten for the `proxyRes` event on `proxy`.
y.on('proxyRes', function (res) {
nsole.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set secure: true
in the options.
reate the HTTPS proxy server in front of a HTTP server
Proxy.createServer({
rget: {
host: 'localhost',
port: 9009
l: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
isten(8009);
reate the proxy server listening on port 443
Proxy.createServer({
l: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
rget: 'https://localhost:9010',
cure: true // Depends on your needs, could be false.
isten(443);
You can activate the websocket support for the proxy using ws:true
in the options.
reate a proxy server for websockets
Proxy.createServer({
rget: 'ws://localhost:9014',
: true
isten(8014);
Also you can proxy the websocket requests just calling the ws(req, socket, head)
method.
etup our server to proxy standard HTTP requests
proxy = new httpProxy.createProxyServer({
rget: {
host: 'localhost',
port: 9015
proxyServer = http.createServer(function (req, res) {
oxy.web(req, res);
isten to the `upgrade` event and proxy the
ebSocket requests as well.
yServer.on('upgrade', function (req, socket, head) {
oxy.ws(req, socket, head);
yServer.listen(8015);
master
)httpProxy.createProxyServer
supports the following options:
If you are using the proxyServer.listen
method, the following options are also applicable:
path
(useful for proxying to proxies)m test
Logo created by Diego Pasquali
The MIT License (MIT)
Copyright (c) 2010 - 2013 Nodejitsu Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.