简体   繁体   中英

How to convert node.js code to regular browser javascript?

I don't know how to get a javascript file to work for web-browser functionalit, when it's coded as node.js.

The code in question is from a github graphenejs-lib . I want to convert this node.js code into js:

import {Apis} from "graphenejs-ws";
var {ChainStore} = require("graphenejs-lib");
Apis.instance("wss://bitshares.openledger.info/ws", true).init_promise.then((res) => {
    console.log("connected to:", res[0].network);
    ChainStore.init().then(() => {
        ChainStore.subscribe(updateState);
    });
});
let dynamicGlobal = null;
function updateState(object) {
    dynamicGlobal = ChainStore.getObject("2.1.0");
    console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal);
}

There is another github from the same developer, steemjs-lib , that shows both the node.js use, and the browser use in the README default page.

I don't know how to make graphenejs-lib into browser javascript, like the steemjs-lib has been made into a regular javascript working version. I contact the dev, but have yet to receive a response.

I figured other people actually know how to do what the dev did for steemjs-lib, and get the graphenejs-lib to work in a browser.

Can you help me out? Thank you.

You can use Browserify to help you with that:

Note that not all of the Node's APIs will be available in the browser.

Use browserify. https://wzrd.in/ will package it for you if you just want to use that one library. https://wzrd.in/standalone/graphenejs-lib@latest

<script src="https://wzrd.in/standalone/graphenejs-lib@latest"></script>
<script>
  const {ChainStore} = graphenejsLib;
  ...
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM