<p align="center">
<img src="https://raw.github.com/nodejitsu/node-http-proxy/master/doc/logo.png"/>
</p>
node-http-proxy
=======
`node-http-proxy` is an HTTP programmable proxying library that supports
websockets. It is suitable for implementing components such as
proxies and load balancers.
### Build Status
<p align="center">
<a href="https://travis-ci.org/nodejitsu/node-http-proxy" target="_blank">
<img src="https://travis-ci.org/nodejitsu/node-http-proxy.png"/></a>
<a href="https://coveralls.io/r/nodejitsu/node-http-proxy" target="_blank">
<img src="https://coveralls.io/repos/nodejitsu/node-http-proxy/badge.png"/></a>
</p>
### Looking to Upgrade from 0.8.x ? Click [here](UPGRADING.md)
### Core Concept
A new proxy is created by calling `createProxyServer` and passing
an `options` object as argument ([valid properties are available here](lib/http-proxy.js#L33-L50))
```javascript
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer(options);
```
An object will be returned with four values:
* web `req, res, [options]` (used for proxying regular HTTP(S) requests)
* ws `req, socket, head, [options]` (used for proxying WS(S) requests)
* listen `port` (a function that wraps the object in a webserver, for your convenience)
* close `[callback]` (a function that closes the inner webserver and stops listening on given port)
It is then possible to proxy requests by calling these functions
```javascript
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
```javascript
proxy.on('error', function(e) {
...
});
```
or using the callback API
```javascript
proxy.web(req, res, { target: 'http://mytarget.com:8080' }, function(e) { ... });
```
When a request is proxied it follows two different pipelines ([available here](lib/http-proxy/passes))
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.
#### Setup a basic stand-alone proxy server
```js
var 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);
//
// 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);
```
#### Setup a stand-alone proxy server with custom server logic
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.
```js
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:5060' });
});
console.log("listening on port 5050")
server.listen(5050);
```
#### Modify a response from a proxied server
Sometimes when you have received a HTML/XML document from the server of origin you would like to modify it before forwarding it on.
[Harmon](https://github.com/No9/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 proxy request header re-writing
This example shows how you can proxy a request using your own HTTP server that
modifies the outgoing proxy request by adding a special header.
```js
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:5060'
});
});
console.log("listening on port 5050")
server.listen(5050);
```
#### Setup a stand-alone proxy server with latency
```js
var 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);
```
#### Listening for proxy events
* `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.
* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
* `close`: This event is emitted once the proxy websocket was closed.
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
```js
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);
});
//
// Listen for the `close` event on `proxy`.
//
proxy.on('close', function (req, socket, head) {
// view disconnected websocket connections
console.log('Client disconnected');
});
```
#### Using HTTPS
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.
##### HTTPS -> HTTP
```js
//
// 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
```js
//
// Create the proxy server l
没有合适的资源?快使用搜索试试~ 我知道了~
postman 一般用于接口测试,现在共享给大家
共1292个文件
js:1059个
h:70个
png:17个
需积分: 1 22 下载量 179 浏览量
2019-02-13
10:26:57
上传
评论 1
收藏 68.07MB ZIP 举报
温馨提示
postman 一般用于接口测试,现在共享给大家;postman 一般用于接口测试,现在共享给大家;postman 一般用于接口测试,现在共享给大家;
资源推荐
资源详情
资源评论
收起资源包目录
postman 一般用于接口测试,现在共享给大家 (1292个子文件)
atom.asar 158KB
hello.bas 120B
snapshot_blob.bin 642KB
natives_blob.bin 430KB
hello.c 605B
CodeDirectory 173B
CodeDirectory 173B
CodeRequirements 192B
CodeRequirements 192B
CodeResources 265KB
CodeResources 7KB
CodeResources 3KB
CodeResources 3KB
CodeResources 3KB
CodeResources 2KB
CodeResources 2KB
CodeResources 2KB
CodeSignature 8KB
CodeSignature 8KB
router.coffee 20KB
test_router.coffee 5KB
mk-server.coffee 4KB
info.coffee 2KB
server.coffee 2KB
scgi.coffee 1KB
hello.coffee 1KB
hello.cpp 1KB
crashpad_handler 895KB
proxy.crt 810B
application.css 4KB
embedded_ga_host.css 34B
icudtl.dat 9.73MB
libnode.dylib 16.13MB
Electron Framework 63.32MB
Electron Framework 63.32MB
spinner.gif 2KB
RACSignal+Operations.h 32KB
metamacros.h 30KB
RACStream.h 13KB
RACSignal.h 9KB
MTLManagedObjectAdapter.h 9KB
MTLJSONAdapter.h 7KB
RACScheduler.h 7KB
RACSequence.h 6KB
RACTuple.h 5KB
MTLModel.h 5KB
MTLModel+NSCoding.h 5KB
SQRLUpdater.h 5KB
RACCommand.h 5KB
NSObject+RACPropertySubscribing.h 5KB
RACKVOChannel.h 4KB
EXTScope.h 4KB
NSValueTransformer+MTLPredefinedTransformerAdditions.h 4KB
ReactiveCocoa.h 4KB
NSObject+RACSelectorSignal.h 4KB
RACBacktrace.h 3KB
RACChannel.h 3KB
NSObject+RACLifting.h 3KB
EXTKeyPathCoding.h 2KB
RACSubscriptingAssignmentTrampoline.h 2KB
RACMulticastConnection.h 2KB
NSObject+RACAppKitBindings.h 2KB
RACCompoundDisposable.h 2KB
RACSubscriber.h 2KB
RACSerialDisposable.h 2KB
RACEvent.h 2KB
RACTestScheduler.h 1KB
RACQueueScheduler+Subclass.h 1KB
NSDictionary+RACSequenceAdditions.h 1KB
NSObject+RACDeallocating.h 1022B
MTLValueTransformer.h 991B
SQRLUpdate.h 972B
SQRLDownloadedUpdate.h 963B
RACDisposable.h 941B
RACScheduler+Subclass.h 939B
NSArray+MTLManipulationAdditions.h 873B
Mantle.h 832B
NSUserDefaults+RACSupport.h 817B
NSDictionary+MTLManipulationAdditions.h 807B
NSURLConnection+RACSupport.h 730B
RACTargetQueueScheduler.h 706B
RACReplaySubject.h 656B
NSString+RACSupport.h 608B
Squirrel.h 587B
NSControl+RACTextSignalSupport.h 568B
NSBundle+SQRLVersionExtensions.h 560B
NSControl+RACCommandSupport.h 560B
NSValueTransformer+MTLInversionAdditions.h 553B
RACSubject.h 548B
NSString+RACSequenceAdditions.h 547B
NSData+RACSupport.h 536B
NSIndexSet+RACSequenceAdditions.h 523B
RACQueueScheduler.h 521B
RACBehaviorSubject.h 517B
NSOrderedSet+RACSequenceAdditions.h 496B
NSArray+RACSequenceAdditions.h 486B
RACScopedDisposable.h 486B
NSSet+RACSequenceAdditions.h 482B
NSEnumerator+RACSequenceAdditions.h 479B
RACGroupedSignal.h 427B
共 1292 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
蓝色温暖
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功