You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dc...@apache.org on 2019/10/08 22:06:36 UTC

[thrift] branch master updated: Add Node.js example for browsers

This is an automated email from the ASF dual-hosted git repository.

dcelasun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 8098428  Add Node.js example for browsers
8098428 is described below

commit 80984289011604d43ec3282a3e8464fa08765f27
Author: HIRANO Satoshi <ha...@gmail.com>
AuthorDate: Wed Oct 9 07:06:30 2019 +0900

    Add Node.js example for browsers
    
    Client: js
    
    This closes #1864.
---
 lib/nodejs/README.md | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/lib/nodejs/README.md b/lib/nodejs/README.md
index 50acfbd..ed306c1 100644
--- a/lib/nodejs/README.md
+++ b/lib/nodejs/README.md
@@ -68,3 +68,44 @@ Since JavaScript represents all numbers as doubles, int64 values cannot be accur
 ## Client and server examples
 
 Several example clients and servers are included in the thrift/lib/nodejs/examples folder and the cross language tutorial thrift/tutorial/nodejs folder.
+
+## Use on browsers
+
+You can use code generated with js:node on browsers with Webpack. Here is an example.
+
+thrift --gen js:node,ts,es6,with_ns
+
+```
+import * as thrift from 'thrift/browser';
+import { MyServiceClient } from '../gen-nodejs/MyService';
+
+let host = window.location.hostname;
+let port = 443;
+let opts = {
+  transport: thrift.TBufferedTransport,
+  protocol: thrift.TJSONProtocol, 
+    headers: {
+     'Content-Type': 'application/vnd.apache.thrift.json',
+    },
+    https: true,
+    path: '/url/path',
+    useCORS: true,
+};
+
+let connection = thrift.createXHRConnection(host, port, opts);
+let thriftClient = thrift.createXHRClient(MyServiceClient, connection);
+
+connection.on('error', (err) => {
+  console.error(err);
+});
+
+thriftClient.myService(param)
+  .then((result) => {
+    console.log(result);
+  })
+  .catch((err) => {
+    ....
+  });
+```
+
+Note that thrift/index.js must be renamed or skipped for browsers.