1

I am trying to host my dapp on Heroku and smart contract on Rinkeby network. My dapp has a front end in node.js and jquery and an express backend server that I use to listen to solidity events and to 'get' and 'put' data from 'Mlab' mongodb offchain server. The app works perfectly fine on my Local Ganache and my server.js and below line of code to get the web3 instance.

const provider = new Web3.providers.HttpProvider("HTTP://127.0.0.1:8545");

My question is while deploying the server.js to Heroku should I keep this web3 provider setting same or does it need to be changed to some other value, please suggest.

0

You should first check if the user already has a web3 provider like MetaMask enabled. If so, use that. Otherwise, you can use something like infura to act as your Web3 provider if that makes sense. From MetaMask - Developers.md:

window.addEventListener('load', function() {

  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    web3 = new Web3(web3.currentProvider);
  } else {
    console.log('No web3? You should consider trying MetaMask!')
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/<your-token>"));
  }

  // Now you can start your app & access web3 freely:
  startApp()

})

Note that you may require the user to interact with your dApp, and in that situation, it may not make sense to enable Web3 through infura, which is best as a read tool. If that is the case, have the fallback logic show a message to the user to install MetaMask, and do not connect to a Web3 provider.

  • I believe your answer will hold good for front end scenario. My question is related to identification of web3 in backend express server when it has been deployed to a hosting site. – tanmay Aug 25 '18 at 17:14
  • Unless you want to spin up your own node, you probably want to use Infura. – Shawn Tabrizi Aug 25 '18 at 19:14
  • I do not want to start my own node, rather I want to run my entire dapp on Rinkeby network. – tanmay Aug 25 '18 at 19:19
  • Then you probably want to use Infura. – Shawn Tabrizi Aug 25 '18 at 19:40
  • Can you please share some articles that explain usage of Infura to connect the backend server with Rinkeby Testnet ? – tanmay Aug 25 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.