摘要简介:在一家新公司做电子商务,他们喜欢用java做API服务,前端则是用PHP进行展示,而我本人不是太喜欢Java这种强类型的语言(当然除了做Native APP之外),写个功能略显啰嗦,后面推荐他们用Nodejs实现一些服务,但是他们给我的答案却是No,当我问及其原因时,他们只给我回答:Java是安全可靠的,稳定且跨平台,这句话让我彻底无语,也许一些对nodejs的热爱者也会遇到类似情况…看了@sumory 的文章后,我决定用thrift对Java和Nodejs做通信,废话也讲完了,现在来介绍下怎么使用吧. nodejs thrift地址:https://github.com/apache/thrift/tree/trunk/lib/nodejs
- 首先需要安装一个nodejs thrift包 npm install thrift --save(ps:我安装的版本是:[email protected])
- 需要安装mocha作为thrif文件测试 npm install mocha --save
- 需要在Java端编译API为Nodejs,命令 thrift --gen js:node thrift_file (ps: java端使用thrift0.6)
实现node.js编写的server API 比如大家通过thrift已经生成gen-nodejs目录,里面包含AccountService和AccountCenter_types文件 建立thrift连接文件:
connection.coffee
thrift = require 'thrift'
module.exports = (settings) ->
connection = thrift.createConnection settings.host, settings.port
connection.on "connect", (err) ->
console.log 'thrift server no start:' + err if err
thrift.coffee
config = require __basename + '/config/config'
connection = require './connection'
module.exports = connection config.thrift
建立nodejs thrift api lib 文件 users.coffee
thrift = require 'thrift'
user = require __basename + '/helpers/gen-nodejs/AccountService'
usertypes = require __basename + '/helpers/gen-nodejs/AccountCenter_types'
server = require __basename + '/connect/thrift/thrift'
client = thrift.createClient user, server
module.exports =
registerUser : (data, fn) ->
data.sex = new usertypes.SexVO(data.sex) if data.sex
client.registerUser new usertypes.UserVO(data), (err, results) ->
fn err, results
changePassword : (data..., fn) ->
client.changePassword data[0], data[1], data[2], (err, results) ->
fn err, results
getUserByEmail : (email, fn) ->
client.getUserByEmail email, (err, results) ->
fn err, results
getUserById : (userId, fn) ->
client.getUserById userId, (err, results) ->
fn err, results
getUserByNickname : (nickname, fn) ->
client.getUserByNickname nickname, (err, results) ->
fn err, results
建立好thrift的users.coffee,我们可以建立user API外部接口,以下是routers.coffee文件
app = require './app'
# OAuth
app.use '/oauth', require './routes/oauth'
# app.use '/baby', require './routes/baby'
app.use '/baby_api', require './routes/baby_api'
建立routers文件夹,在routers文件夹下建立baby_api文件夹,然后这里分别建立index.coffee和users.coffee文件 index.coffee (router 列表文件)
express = require 'express'
module.exports = app = express()
# User Accounts API
users = require './users'
app.post '/users/register', users.registerUser
app.post '/users/changepassword', users.changePassword
在mocha中建立测试文件:
users = require __basename + '/lib/thrift/users'
test =
id : 17689
email : 'fioman[@qq](/user/qq).com'
describe 'getUserByEmail[@account](/user/account)', ->
it 'should get many acounts info', (done) ->
users.getUserByEmail test.email, (err, results) ->
throw err if err
results.should.have.property('email')
results.should.be.json
results.id.should.be.a 'number'
results.email.should.be.a 'string'
results.email.should.be.equal test.email
done()
describe 'getUserById[@account](/user/account)', ->
it 'should get many acounts info', (done) ->
users.getUserById test.id, (err, results) ->
throw err if err
results.should.have.property('id')
results.should.be.json
results.id.should.be.a 'number'
results.email.should.be.a 'string'
results.id.should.be.equal test.id
done()
describe 'getUserByNickname[@account](/user/account)', ->
it 'should get many acounts info', (done) ->
users.getUserByNickname 'fioman', (err, results) ->
throw err if err
results.should.have.property('nickname')
results.should.be.json
results.id.should.be.a 'number'
results.email.should.be.a 'string'
results.nickname.should.be.equal 'fioman'
done()
成功后先npm test一遍 目前项目托管在oschina git中 http://git.oschina.net/ym/baby_mobile_api