惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
量子位
B
Blog
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
T
The Exploit Database - CXSecurity.com
N
News | PayPal Newsroom
Cloudbric
Cloudbric
A
About on SuperTechFans
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
有赞技术团队
有赞技术团队
H
Heimdal Security Blog
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
IT之家
IT之家
Know Your Adversary
Know Your Adversary
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
B
Blog RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
博客园 - 叶小钗
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
The Hacker News
The Hacker News
Y
Y Combinator Blog
I
Intezer
The Register - Security
The Register - Security
F
Full Disclosure
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler

博客园 - 草屋主人

高性能队列Fqueue的设计和使用实践 软件系统开发中的数据交换协议 MySQL与NoSQL——SQL与NoSQL的融合 NoSQL架构实践(三)——以NoSQL为缓存 NoSQL架构实践(二)——以NoSQL为主 NoSQL架构实践(一)——以NoSQL为辅 移动互联网系统架构的特点 关系数据库还是NoSQL数据库 NoSQL开篇——为什么要使用NoSQL 利用FUSE把memcached当磁盘使用 velocity大会主题演讲内容“移动互联网内容平台的架构和性能优化” InfoQ百度技术沙龙"APP Engine技术应用"主题的总结记录 用于php二进制操作和socket二进制通讯的phpbuffer v0.1开源发布 iDataForum2010数据库技术论坛总结 O'Reilly Velocity性能和运维大会以及我的演讲主题 基于视觉的系统性能优化 【译】适合dba和开发者的mysql最佳实践 如何用队列提升系统性能 基于memcached协议构建自定义协议
使用node.js快速开发透明代理
草屋主人 · 2010-12-23 · via 博客园 - 草屋主人

           [文章作者:孙立 链接:http://www.cnblogs.com/sunli/ 更新时间:2010-12-23]  

      服务器端js(Server-Side Javascrpt)很早也就有了,JAVA中也有javascript 的script引擎。但是最近服务器端js的火爆确实因为node.js项目。在velocity china 2010大会Douglas Crockford(Yahoo!)也有一个topic《卷土重来:服务器端JavaScript》提到node.js。关于node.js的详细资料请google.

      node.js的非常大的一个特点就是事件驱动,在开发服务器端服务的时候显得非常方便。昨晚在新浪的@timYang也提到了node.js,估计新浪微博也注意到了它的优点。同时,淘宝最近也表示对node.js有相当的兴趣。参见:http://www.tbdata.org/archives/1285  http://www.tbdata.org/archives/1292 ,为了体验一把,就用node.js写了一个透明的代理服务层,代码如下:

var net = require('net');
var proxyhost="127.0.0.1";//被代理的服务的IP
var proxyport=3306;//被代理的端口
var listenport=8124;//代理端口
net.createServer(function (socket) {
socket.on(
"connect",function(){
console.log(
'connected');
try{
var db=net.createConnection(proxyport,proxyhost);
db.on(
"connect",function(){
console.log(
"server connected");
socket.on(
"data", function (data) {
db.write(data);
});
db.on(
"data",function(data){
console.log(data.toString(
'utf8',0,data.legnth));
//console.log(data);
socket.write(data);
});
socket.on(
"close",function(){
console.log(
"server closed");
db.destroy();
});
});
db.on(
"error",function(data){
console.log(
"error:\r\n"+data);
});
db.on(
"end",function(){
console.log(
"server closed");
socket.destroy();
});
}
catch(err){
console.log(err);
}
});
}).listen(listenport,
"0.0.0.0");

     OK,大功告成,只用了一点点的时间,是的,就是这么简单。我们把proxyhost,proxyport=3306改成本机的一个数据上,启动后,我用mysql客户端连8124,就可以连上3306端口的数据库了。

    不仅仅是数据库,把proxyhost,proxyport指向到memcached,那么就变成memcached代理了。

    通过console.log你可以非常方便的调试协议。也可以用来监视某些不开放的协议。

    使用node.js开发网络服务应用程序,比如代理层,httpserver处理非常方便。

    刚刚有朋友提出了一个 bug ,谢谢这位朋友。