背景:公司要用Node与其他语言(Java)写的服务通信。
1,服务端 helloServer.js
var thrift = require(‘thrift‘);
var helloService = require(‘./HelloService‘);
var server = thrift.createServer(helloService, {
hello: function(para, success){
console.log("para: " + para);
success(null, "Hi, Client! I am Server!");
}
}, {});
server.listen(8080);
2,客户端 helloClient.js
var thrift = require(‘thrift‘);
var helloService = require(‘./HelloService‘);
//创建连接和客户端
var connection = thrift.createConnection(‘localhost‘, 8080);
connection.on(‘error‘, function(err) {
console.error(err);
});
var client = thrift.createClient(helloService, connection);
//调用hello方法
var para = ‘Hi Server! I am Client.‘;
client.hello(para, function(err, res){
if(err){
console.error("Error: " + err);
}else{
console.log("Result: " + res);
}
connection.end();
});
3,thrift compiler version: 0.9.2.
4,此版本的node第三方库thrift的server.js文件有个bug,导致thrift.createServer(processor, handler, options)的第三个参数必须得传。

截图红框中的代码应该为:
if (options && options.tls) {

原文:http://my.oschina.net/aaxaac/blog/402585