[Ethereum] Connect to ropsten network from Node.js

metamasknodejsropsten

I need to connect to Ropsten network from my Node.js backend .
I need to read logs and extract data from them and listen to changes in the network for my contract.
The problem is that i don't know how to input credentials of the account that's going to read it (address and private key )

 if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

This is my code from frontend . I'm using metamask and it's getting me the current provider perfectly.
But when changing to Node.js , there's no provider of course (Metamask in my case ) .

So the question is :
How to input Ropsten network instead of "http://localhost:8545" which refers to a private network and how can i input the account's address and secret key ?
Thanks .

Best Answer

You need to set up a ropsten node, either in remote or in local, as you would do for a real Ethereum node.

Simplest answer I can give you for testing is to use the infura.io node. Subscribe to the service and they will send you by mail an url of the form https://ropsten.infura.io/API_KEY. Just input the url in your code like this:

web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/API_KEY"))
Related Topic