在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:http-party/node-http-proxy开源软件地址:https://github.com/http-party/node-http-proxy开源编程语言:JavaScript 100.0%开源软件介绍:node-http-proxy
Table of Contents
Installation
Upgrading from 0.8.x ?Click here Core ConceptA new proxy is created by calling var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer(options); // See (†) †Unless listen(..) is invoked on the object, this does not create a webserver. See below. An object will be returned with four methods:
It is then possible to proxy requests by calling these functions http.createServer(function(req, res) {
proxy.web(req, res, { target: 'http://mytarget.com:8080' });
}); Errors can be listened on either using the Event Emitter API proxy.on('error', function(e) {
...
}); or using the callback API proxy.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 Use CasesSetup a basic stand-alone proxy servervar http = require('http'),
httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†)
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000); †Invoking listen(..) triggers the creation of a web server. Otherwise, just the proxy instance is created. Setup a stand-alone proxy server with custom server logicThis example shows how you can proxy a request using your own HTTP server and also you can put your own logic to handle the request. var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, { target: 'http://127.0.0.1:5050' });
});
console.log("listening on port 5050")
server.listen(5050); Setup a stand-alone proxy server with proxy request header re-writingThis example shows how you can proxy a request using your own HTTP server that modifies the outgoing proxy request by adding a special header. var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
// To modify the proxy connection before data is sent, you can listen
// for the 'proxyReq' event. When the event is fired, you will receive
// the following arguments:
// (http.ClientRequest proxyReq, http.IncomingMessage req,
// http.ServerResponse res, Object options). This mechanism is useful when
// you need to modify the proxy request before the proxy connection
// is made to the target.
//
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
var server = http.createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, {
target: 'http://127.0.0.1:5050'
});
});
console.log("listening on port 5050")
server.listen(5050); Modify a response from a proxied serverSometimes when you have received a HTML/XML document from the server of origin you would like to modify it before forwarding it on. Harmon allows you to do this in a streaming style so as to keep the pressure on the proxy to a minimum. Setup a stand-alone proxy server with latencyvar http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with latency
//
var proxy = httpProxy.createProxyServer();
//
// Create your server that makes an operation that waits a while
// and then proxies the request
//
http.createServer(function (req, res) {
// This simulates an operation that takes 500ms to execute
setTimeout(function () {
proxy.web(req, res, {
target: 'http://localhost:9008'
});
}, 500);
}).listen(8008);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9008); Using HTTPSYou can activate the validation of a secure SSL certificate to the target connection (avoid self-signed certs), just set HTTPS -> HTTP//
// Create the HTTPS proxy server in front of a HTTP server
//
httpProxy.createServer({
target: {
host: 'localhost',
port: 9009
},
ssl: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
}
}).listen(8009); HTTPS -> HTTPS//
// Create the proxy server listening on port 443
//
httpProxy.createServer({
ssl: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
},
target: 'https://localhost:9010',
secure: true // Depends on your needs, could be false.
}).listen(443); HTTP -> HTTPS (using a PKCS12 client certificate)//
// Create an HTTP proxy server with an HTTPS target
//
httpProxy.createProxyServer({
target: {
protocol: 'https:',
host: 'my-domain-name',
port: 443,
pfx: fs.readFileSync('path/to/certificate.p12'),
passphrase: 'password',
},
changeOrigin: true,
}).listen(8000); Proxying WebSocketsYou can activate the websocket support for the proxy using //
// Create a proxy server for websockets
//
httpProxy.createServer({
target: 'ws://localhost:9014',
ws: true
}).listen(8014); Also you can proxy the websocket requests just calling the //
// Setup our server to proxy standard HTTP requests
//
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 9015
}
});
var proxyServer = http.createServer(function (req, res) {
proxy.web(req, res);
});
//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
});
proxyServer.listen(8015); Options
NOTE:
If you are using the
Listening for proxy events
var httpProxy = require('http-proxy');
// Error example
//
// Http Proxy Server with bad target
//
var proxy = httpProxy.createServer({
target:'http://localhost:9005'
});
proxy.listen(8005);
//
// Listen for the `error` event on `proxy`.
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
//
// Listen for the `proxyRes` event on `proxy`.
//
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
//
// Listen for the `open` event on `proxy`.
//
proxy.on('open', function (proxySocket) {
// listen for messages coming FROM the target here
proxySocket.on('data', hybiParseAndLogMessage);
全部评论
专题导读
上一篇:robbiehanson/CocoaHTTPServer: A small, lightweight, embeddable HTTP server for M ...发布时间:2022-06-17下一篇:encode/httpx: A next generation HTTP client for Python. 发布时间:2022-06-17热门推荐
热门话题
阅读排行榜
|
请发表评论