[Ethereum] Connecting to hosted web3 provider from NodeJS backend

infuraropstenweb3-providersweb3js

I'm using these instructions from etherchain. Here's a similar question(seems like duplicate, but it's not). The solution provided there doesn't work for me, because I don't have a local ethereum client to connect to. I want to use one provided by Etherchain.

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://rpc.ethapi.org:8545"));
web3.eth.getBlock("latest", (error, result) => {
  console.log('error:', error);
  console.log('results', result);
});

Error:

Error: Invalid JSON RPC response: ""
   at Object.InvalidResponse (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/web3/lib/web3/httpprovider.js:116:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/home/manid/Рабочий стол/Pogovorim/NODE_SERVER/node_modules/xhr2/lib/xhr2.js:414:24)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at Socket.socketErrorListener (_http_client.js:309:9)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1281:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

What am I missing?

Best Answer

I've replaced

const web3 = new Web3(new Web3.providers.HttpProvider("http://rpc.ethapi.org:8545"));

with:

const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/TOKEN"));

and everything works.

This https://ropsten.infura.io/TOKEN was given to me after registration on infura.io