Nodejs accesses the Java service through thrift, and nodejsthrift
In the previous article, Java is used as the thrift client and server. Next, we will use nodejs as the client. For details about how to install Nodejs, visit http://www.cnblogs.com/xucheng/p/3988835.htmlnodejs.
1. Import thrift.exe into the Directory and execute thrift-0.9.2.exe-gen js: node hello. thrift compilation hello. thrift to generate the nodejs implementation file.
2. Enter the generated gen-nodejs directory in the cmd window, use npm install thrift to install the thrift module of nodejs, and add a node_modules directory after installation.
3. Create a js file as the thrift client. The content is as follows:
// Introduce the thrift Module
Var thrift = require ('thrift ');
// Add the hello service definition file in the same path ./
Var Hello = require ('./Hello. js '),
Ttypes = require ('./hello_types ');
// Create a connection and a client
Var connection = thrift. createConnection ('localhost', 19090 ),
Client = thrift. createClient (Hello, connection );
// Connection
Connection. on ('error', function (err ){
Console. error (err );
});
// Call the helloString Function
Console. log (client. helloString ('tomdog'). toString ());
4. Start the Java server program in the previous article and run nodejsclient. js with the node command. The console output is [object Promise]. Here, js regards the string returned by Java as an object.
5. Of course, there are examples of various languages in the lib folder of thrift.