But the recent popularity of server-side js is indeed due to the node.js project. At the velocity china 2010 conference, Douglas Crockford (Yahoo!) also had a topic "Comes Back: Server-side JavaScript" that mentioned node.js. For detailed information about node.js, please google.
A very big feature of node.js is event-driven, which is very convenient when developing server-side services. @timYang on Sina last night also mentioned node.js, and I guess Sina Weibo also noticed its advantages. At the same time, Taobao has recently expressed considerable interest in node.js. See: http://www.tbdata.org/archives/1285 http://www.tbdata.org/archives/1292 To experience it, use node. js wrote a transparent proxy service layer, the code is as follows:
var net = require('net');
var proxyhost="127.0.0.1";//IP of the proxy service
var proxyport=3306;//Proxied port
var listenport =8124;//Proxy port
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:rn" data);
});
db.on("end",function(){
console.log ("server closed");
socket.destroy();
});
}catch(err){
console.log(err);
}
}) ;
}).listen(listenport, "0.0.0.0");
//For testing, the production environment needs to consider stability code processing
OK, you're done, just use After a little while, yes, it's that simple. We changed the proxyhost and proxyport=3306 to a data on the local machine. After starting, I used the mysql client to connect to 8124, and I can connect to the database on port 3306.
Not just a database, point proxyhost and proxyport to memcached, and it becomes a memcached proxy.
You can debug the protocol very conveniently through console.log. It can also be used to monitor certain protocols that are not open.
Use node.js to develop network service applications, such as the proxy layer, and httpserver processing is very convenient.
A friend just raised a bug, thank you friend.