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://");

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.

1 Answer 1


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:


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
    Commented Aug 25, 2018 at 17:14
  • Unless you want to spin up your own node, you probably want to use Infura. Commented Aug 25, 2018 at 19:14
  • I do not want to start my own node, rather I want to run my entire dapp on Rinkeby network.
    – tanmay
    Commented Aug 25, 2018 at 19:19
  • Then you probably want to use Infura. Commented Aug 25, 2018 at 19:40
  • Can you please share some articles that explain usage of Infura to connect the backend server with Rinkeby Testnet ?
    – tanmay
    Commented Aug 25, 2018 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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