最近在做微服務相關產品,其中需要有API網關相關產品,雖然早有研究過API網關產品TYK,但是感覺其太重,想起之前研究開源BI產品saiku的時候,記得其有NodeJS的代理代碼,於是看了下,頗有啓發,略微修改了一些,拿出來跟大家分享下,雖然簡單,不可用於生產,但是對於學習還是不錯的
package.json
{
"name": "SmartHttpProxy",
"description": "An simplest Http Proxy",
"version": "0.0.1",
"author": "[email protected]",
"license": {
"type": "Apache License Version 2"
},
"main": "server.js",
"scripts": {
"start": "node server.js 8088 127.0.0.1 80 / "
},
"dependencies": {
"express": "~4.13.4"
}
}
server.js
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
* To play with the chaos monkey, set the CHAOS_MONKEY environment variable
* to anything (Preferably a nice name for your chaos monkey).
*
* To start the server, run `node server.js [port] [backend_host] [backend_port]`
*/
// newer versions of node.js use the lower-case argv
var argv = process.ARGV || process.argv;
var http = require('http');
var express = require('express');
var path = require('path');
var app = express();
var port = process.env.C9_PORT || parseInt(argv[2], 10) || 8088;
var backend_host = argv[3] || 'httpbin.org';
var backend_port = argv[4] || 80;
var backend_path_prefix = argv[5] || '/';
var auth = argv[6] || null;
var standard_prefix = "/test/";
// Proxy request
function get_from_proxy(request, response) {
// if a path prefix is set, remove the existing one
if (backend_path_prefix !== '') {
if (request.url.indexOf(standard_prefix) === 0) {
request.url = backend_path_prefix + request.url.substr(standard_prefix.length);
}
}
if (auth) {
request.headers['authorization'] = 'Basic ' + new Buffer(auth).toString('base64');
request.headers['www-authorization'] = 'Basic ' + new Buffer(auth).toString('base64');
delete request.headers['cookie'];
}
var options = {
hostname : backend_host,
port : backend_port,
path : request.url,
method : request.method,
headers : request.headers
};
console.log(options.method, options.path);
var proxy_request = http.request(options);
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
request.addListener('end', function() {
proxy_request.end();
});
proxy_request.addListener('error', function (error) {
console.log("ERROR:",error);
});
proxy_request.addListener('response', function (proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
}
// Handle incoming requests
app.all("/test/*", function(request, response) {
request.headers.host = backend_host;
get_from_proxy(request, response);
});
console.log("Connected to '", backend_host, ":", backend_port,"'");
console.log("Proxy listening on", port);
app.listen(port, '0.0.0.0');