最近在做微服务相关产品,其中需要有API网关相关产品,虽然早有研究过API网关产品TYK,但是感觉其太重,想起之前研究开源BI产品saiku的时候,记得其有NodeJS的代理代码,于是看了下,颇有启发,略微修改了一些,拿出来跟大家分享下,虽然简单,不可用于生产,但是对于学习还是不错的
package.json
{
"name": "SmartHttpProxy",
"description": "An simplest Http Proxy",
"version": "0.0.1",
"author": "liuyg@liuyingguang.cn",
"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;
//要代理的URL的域名
var backend_host = argv[3] || 'httpbin.org';
//要代理的URL的访问端口
var backend_port = argv[4] || 80;
//要代理的URL的路径
var backend_path_prefix = argv[5] || '/';
//要代理的URL的认证信息
var auth = argv[6] || null;
//拦截替换的路径信息,eg. 如果访问路径为:http://localhost:8088/test/ip,此值设置为“/test/”,则会将其处理为:http://{domain}:{port}/{backend_path_prefix}/ip
//即将path中的第一个与standard_prefix匹配的字符串替换为standard_prefix,如上例子中,处理的结果为:http://httpbin.org/ip
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');
- 微服务指南走北(一):微服务是什么
- 微服务指南走北(二):微服务架构的进程间通信(IPC)
- 微服务指南走北(三):Restful API 设计简述
- 微服务指南走北(四):你不愿意做微服务架构的十个理由
微服务指南走北(五):什么样的服务才可以说是微服务?
by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(==防止爬虫==):http://blog.liuyingguang.cn