Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/connect'
Browse files Browse the repository at this point in the history
Conflicts:
	server.js
  • Loading branch information
switer committed Dec 30, 2013
2 parents 8dd3827 + 36b107e commit b0b959a
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/test/connect
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## ChangeLog for: http-route-proxy

#### Version 0.1.1 - 2013/12/30

- [Feature]: Support using as express connect middleware

#### Version 0.1.0 - 2013/12/28

- [Feature]: Forward to various host on matching the path action.
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,30 @@ proxyServer.proxy([
]);
```

#### Using as express connect middleware
__express app.js config:__
Push it on the first of connect

```javascript
app.use(proxyServer.connect({
to: 'www.google.com',
https: true,
route: ['/']
}));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
```

### Change Log

#### Version 0.1.1 - 2013/12/30

- [Feature]: Support using as express connect middleware

#### Version 0.1.0 - 2013/12/28

- [Feature]: Forward to various host on matching the path action.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-route-proxy",
"version": "0.1.0",
"version": "0.1.1",
"description": "Convenient HTTP proxy for cross-domail request, request forward and creating mock data.",
"main": "server.js",
"scripts": {
Expand Down
182 changes: 116 additions & 66 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ var httpProxy = require('http-proxy'), // base on nodejitsu proxy server

var server = {

/**
* request proxy route match status code
*/
status: {

STATIC: 0,
FORWARD: 1,
UNMATCHED: 2
},
/**
* id for each proxy server
*/
Expand Down Expand Up @@ -62,21 +71,44 @@ var server = {
* Proxy enter api
*/
proxy: function (hosts) {
var _this = this;

// handle each proxy config
_.each(hosts, function (hostConfig) {

var hostObj = {
from: _this.parseHost(hostConfig.from),
routes: _this.parseRouteRule(hostConfig)
from: this.parseHost(hostConfig.from),
routes: this.parseRouteRule(hostConfig)
}

var serverId = _this.create(hostObj.from, hostObj.routes, hostConfig);
_this.saveHost(hostObj.from, hostObj.routes, serverId);
var serverId = this.create(hostObj.from, hostObj.routes, hostConfig);
this.saveHost(hostObj.from, hostObj.routes, serverId);

});
}.bind(this));
},
/**
* support as express connect middleware
*/
connect: function (config) {

var proxy = new httpProxy.RoutingProxy(),
connectConfig = {
rules: this.parseRouteRule(config)
},
_this = this;

return function (req, res, next) {

_this.proxyMiddleware(req, res, proxy, connectConfig, function (proxyStatus) {
// processing in express
if (proxyStatus.status !== _this.status.FORWARD) {
next();
}
// forward request
else {
console.log(proxyStatus.message);
}
});
}
},
/**
* create a server for the one proxy config
Expand Down Expand Up @@ -108,79 +140,97 @@ var server = {
},
function (req, res, proxy) {

var method = req.method,
requestURL = req.url,
url = method.toUpperCase() + ':' + req.url,
forwardRouteObj = null;

// get proxy config by proxy server id
var proxyConfig = _this.proxies[serverId];

if (_this.staticMatched(url, proxyConfig.rules.static)) {
// match route is static file response
var directory = path.resolve(process.cwd(), '.');

// send static files without server
staticServer.sendfile(req, res, directory);

console.log(method.blue.grey + ' ' + requestURL + ' from '.green.grey + from.hostname + ':' + from.port.toString().blue.grey);
_this.proxyMiddleware(req, res, proxy, _this.proxies[serverId], function (statusObj) {
console.log(statusObj.message);
});

} else if (forwardRouteObj = _this.forwardMatched(url, proxyConfig.rules.forward)) {
// set headers of config
_this.setHeaders(req, res, forwardRouteObj.options.headers);

var forwardObj = forwardRouteObj.forward,
// forward options
proxyForwardOptions = {
changeOrigin: true,
host: forwardObj.hostname,
port: forwardObj.port
};

if (forwardRouteObj.options.https) {
// set https forward options
proxyForwardOptions = _.extend(proxyForwardOptions, {
target: {
https: true,
rejectUnauthorized: false
}
});
}

// forward to remote server
proxy.proxyRequest(req, res, proxyForwardOptions);

console.log('forward '.yellow.grey + method.blue.grey + ' ' + requestURL + ' from '.green.grey + from.hostname + ':' +
from.port.toString().blue.grey +' to '.green.grey + forwardObj.protocal + '://' + forwardObj.hostname + ':' + forwardObj.port.toString().blue.grey +
requestURL);
}
else {
console.log("Sorry proxy error, http-route-proxy can't match any forward rule, please check your proxy config".red);
}
/**
* must listen hostname, otherwise it will be fail when repeat listening
* localhost in the some port
*/
})
.listen(from.port, from.hostname)
.on('error', function (e) {
// catch server listen error
console.log('Create proxy error,'.red.grey + "cause by can't listen host from " +
from.hostname.yellow.grey + ' : ' + from.port.toString().blue.grey);
});
/**
* must listen hostname, otherwise it will be fail when repeat listening
* localhost in the some port
*/
}).listen(from.port, from.hostname)
.on('error', function (e) {
// catch server listen error
console.log('Create proxy error,'.red.grey + "cause by can't listen host from " +
from.hostname.yellow.grey + ' : ' + from.port.toString().blue.grey);
});

console.log('Listen from ' + from.hostname.green.grey + ' : ' + from.port.toString().blue.grey);

// Custom error
server.proxy.on('proxyError', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});

res.end('Something went wrong. And we are reporting a custom error message.\n' + err);
});

return this.serverId ++;
},
/**
* Proxy middle for handling request and response
*/
proxyMiddleware: function (req, res, proxy, config, next) {
var from = this.parseHost(req.headers.host),
method = req.method,
requestURL = req.url,
url = method.toUpperCase() + ':' + req.url,
forwardRouteObj = null;

// get proxy config by proxy server id
var proxyConfig = config;

if (this.staticMatched(url, proxyConfig.rules.static)) {
// match route is static file response
var directory = path.resolve(process.cwd(), '.');

// send static files without server
staticServer.sendfile(req, res, directory);

next({
status: this.status.STATIC,
message: method.blue.grey + ' ' + requestURL + ' from '.green.grey + from.hostname + ':' + from.port.toString().blue.grey
});

} else if (forwardRouteObj = this.forwardMatched(url, proxyConfig.rules.forward)) {
// set headers of config
this.setHeaders(req, res, forwardRouteObj.options.headers);

var forwardObj = forwardRouteObj.forward,
// forward options
proxyForwardOptions = {
changeOrigin: true,
host: forwardObj.hostname,
port: forwardObj.port
};

if (forwardRouteObj.options.https) {
// set https forward options
proxyForwardOptions = _.extend(proxyForwardOptions, {
target: {
https: true,
rejectUnauthorized: false
}
});
}

// forward to remote server
proxy.proxyRequest(req, res, proxyForwardOptions);

next({
status: this.status.FORWARD,
message: 'forward '.yellow.grey + method.blue.grey + ' ' + requestURL + ' from '.green.grey + from.hostname + ':' +
from.port.toString().blue.grey +' to '.green.grey + forwardObj.protocal + '://' + forwardObj.hostname + ':' +
forwardObj.port.toString().blue.grey + requestURL
});
}
else {
next({
status: this.status.UNMATCHED,
message: "Sorry proxy error, http-route-proxy can't match any forward rule, please check your proxy config".red
});
}
},
/**
* Set headers by proxy config
*/
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ proxyServer.proxy([
// },
{
from: 'localhost:9004',
to: 'https://www.12306.cn'
to: 'https://kyfw.12306.cn'
},
{
from: 'localhost:9003',
Expand All @@ -24,8 +24,8 @@ proxyServer.proxy([
forward: 'localhost:9004',
headers: {
req: {
origin: 'https://www.12306.cn/',
referer: 'https://www.12306.cn/'
origin: 'https://kyfw.12306.cn/',
referer: 'https://kyfw.12306.cn/'
}
}
}, {
Expand Down

0 comments on commit b0b959a

Please sign in to comment.