0

I have a problem with using : web3.net.listening The case when I am connected to the testRPC is working and it returns me TRUE. But the case I am not connected it returns me INTERNAL SERVER ERROR. I understand that this "listening" object doesn't exist when I am no longuer connected to testRPC. How can I solve this? so I can get false as result if testrpc not connected.

const config = require('config');
const Router = require('koa-router');
const router = new Router();

const Web3 = require('web3');
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(config.provider.url));

router.get('/cbn/provider-status', ctx => {

    const listening = web3.net.listening;
    if (listening){
        ctx.body = {"status":listening}
    }
    else ctx.body = {"status":"false"}

})

Many Thanks in advance.

1
  • 1
    you can use web3.isConnected() to check if connection to node exists or not.
    – kherwa
    Commented May 16, 2018 at 9:47

1 Answer 1

1

Here is the answer for that issue:

router.get('/cbn/provider-status', ctx => {
    ctx.body = {"status":web3.isConnected()}
})

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.