summaryrefslogtreecommitdiff
path: root/lib/nodejs
diff options
context:
space:
mode:
authorHIRANO Satoshi <happy.hirano@gmail.com>2019-10-09 07:06:30 +0900
committerDuru Can Celasun <dcelasun@apache.org>2019-10-08 23:06:30 +0100
commit80984289011604d43ec3282a3e8464fa08765f27 (patch)
tree2e3011536939fcafa39891a7d6bdbdf9c033a013 /lib/nodejs
parentd4c6b5632ffd4792a89d845592aa953573173f8f (diff)
downloadthrift-80984289011604d43ec3282a3e8464fa08765f27.tar.gz
Add Node.js example for browsers
Client: js This closes #1864.
Diffstat (limited to 'lib/nodejs')
-rw-r--r--lib/nodejs/README.md41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/nodejs/README.md b/lib/nodejs/README.md
index 50acfbdbe..ed306c15f 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.