THRIFT-2493:Node.js lib needs HTTP client
Client: node
Patch: Randy Abernethy
Clean up of many jshint warnings/errors, jsdoc for HttpConnect,
added support for https and Python to HttpConnect, added tests
for HttpConnect with https and promises.
diff --git a/lib/nodejs/examples/README.md b/lib/nodejs/examples/README.md
index 0773e60..db012df 100644
--- a/lib/nodejs/examples/README.md
+++ b/lib/nodejs/examples/README.md
@@ -18,6 +18,7 @@
#Generate the bindings:
../../../compiler/cpp/thrift --gen js:node user.thrift
+../../../compiler/cpp/thrift --gen js:node --gen py hello.thrift
#To run the user example, first start up the server in one terminal:
NODE_PATH=../lib:../lib/thrift node server.js
@@ -28,5 +29,6 @@
#For an example using JavaScript in the browser to connect to
#a node.js server look at hello.html, hello.js and hello.thrift
-
+#HTTP examples are provided also: httpClient.js and httpServer.js
+#You can test HTTP cross platform with the httpServer.py Python server
diff --git a/lib/nodejs/examples/httpClient.js b/lib/nodejs/examples/httpClient.js
new file mode 100644
index 0000000..19cc0c3
--- /dev/null
+++ b/lib/nodejs/examples/httpClient.js
@@ -0,0 +1,23 @@
+var thrift = require('thrift');
+var helloSvc = require('./gen-nodejs/HelloSvc.js');
+
+var options = {
+ transport: thrift.TBufferedTransport,
+ protocol: thrift.TJSONProtocol,
+ path: "/hello",
+ headers: {"Connection": "close"},
+ https: false
+};
+
+var connection = thrift.createHttpConnection("localhost", 9090, options);
+var client = thrift.createHttpClient(helloSvc, connection);
+
+connection.on("error", function(err) {
+ console.log("Error: " + err);
+});
+
+client.hello_func(function(error, result) {
+ console.log("Msg from server: " + result);
+});
+
+
diff --git a/lib/nodejs/examples/httpServer.js b/lib/nodejs/examples/httpServer.js
new file mode 100644
index 0000000..acae136
--- /dev/null
+++ b/lib/nodejs/examples/httpServer.js
@@ -0,0 +1,31 @@
+var thrift = require('thrift');
+var helloSvc = require('./gen-nodejs/HelloSvc');
+
+//ServiceHandler: Implement the hello service
+var helloHandler = {
+ hello_func: function (result) {
+ console.log("Received Hello call");
+ result(null, "Hello from Node.js");
+ }
+};
+
+//ServiceOptions: The I/O stack for the service
+var helloSvcOpt = {
+ handler: helloHandler,
+ processor: helloSvc,
+ protocol: thrift.TJSONProtocol,
+ transport: thrift.TBufferedTransport
+};
+
+//ServerOptions: Define server features
+var serverOpt = {
+ services: {
+ "/hello": helloSvcOpt
+ }
+}
+
+//Create and start the web server
+var port = 9090;
+thrift.createWebServer(serverOpt).listen(port);
+console.log("Http/Thrift Server running on port: " + port);
+
diff --git a/lib/nodejs/examples/httpServer.py b/lib/nodejs/examples/httpServer.py
new file mode 100644
index 0000000..b8ba586
--- /dev/null
+++ b/lib/nodejs/examples/httpServer.py
@@ -0,0 +1,19 @@
+import sys
+sys.path.append('gen-py')
+
+from hello import HelloSvc
+from thrift.protocol import TJSONProtocol
+from thrift.server import THttpServer
+
+class HelloSvcHandler:
+ def hello_func(self):
+ print "Hello Called"
+ return "hello from Python"
+
+processor = HelloSvc.Processor(HelloSvcHandler())
+protoFactory = TJSONProtocol.TJSONProtocolFactory()
+port = 9090
+server = THttpServer.THttpServer(processor, ("localhost", port), protoFactory)
+print "Python server running on port " + str(port)
+server.serve()
+