Lighting@刘迎光
相信了,才有可能遇见,不相信,也许只会是擦肩而过!
Toggle navigation
Lighting@刘迎光
首页
IT技术
微服务(IT)
技术问答
OpenBI
读书笔记
公众号【今日脑图】
关于我
自媒体
归档
标签
微服务之使用NodeJS创建HTTP代理服务器(反向代理)
微服务
MicroService
nodejs
http
反向代理
2017-07-19 20:20:45
933
lightingfire
微服务
MicroService
nodejs
http
反向代理
最近在做微服务相关产品,其中需要有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'); ``` * [微服务指南走北(一):微服务是什么](http://blog.liuyingguang.cn/blog/post/lightingfire/c82b6a3a7e55) * [微服务指南走北(二):微服务架构的进程间通信(IPC)](http://blog.liuyingguang.cn/blog/post/lightingfire/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%8C%87%E5%8D%97%E8%B5%B0%E5%8C%97%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E7%9A%84%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%EF%BC%88IPC%EF%BC%89) * [微服务指南走北(三):Restful API 设计简述](http://blog.liuyingguang.cn/blog/post/lightingfire/Restful-API-%E8%AE%BE%E8%AE%A1%E7%AE%80%E8%BF%B0) * [微服务指南走北(四):你不愿意做微服务架构的十个理由](http://blog.liuyingguang.cn/blog/post/lightingfire/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%8C%87%E5%8D%97%E8%B5%B0%E5%8C%97%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9A%E4%BD%A0%E4%B8%8D%E6%84%BF%E6%84%8F%E5%81%9A%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E7%9A%84%E5%8D%81%E4%B8%AA%E7%90%86%E7%94%B1) [微服务指南走北(五):什么样的服务才可以说是微服务?](http://blog.liuyingguang.cn/blog/post/lightingfire/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%8C%87%E5%8D%97%E8%B5%B0%E5%8C%97%EF%BC%88%E4%BA%94%EF%BC%89%EF%BC%9A%E4%BB%80%E4%B9%88%E6%A0%B7%E7%9A%84%E6%9C%8D%E5%8A%A1%E6%89%8D%E5%8F%AF%E4%BB%A5%E8%AF%B4%E6%98%AF%E5%BE%AE%E6%9C%8D%E5%8A%A1%EF%BC%9F) --- > by 刘迎光@萤火虫工作室 > OpenBI交流群:495266201 > MicroService 微服务交流群:217722918 > mail: liuyg#liuyingguang.cn > 博主首页(==防止爬虫==):http://blog.liuyingguang.cn
Pre:
构建属于自己的elasticsearch Docker镜像
Next:
eclipse安装nodejs插件nodeclipse
Table of content