Search code examples
solanasolana-web3js

Solana ECONNREFUSED error on localhost for the simple airdrop transaction


I launched my local solana environment with 'solana-test-validator' command, I have a simple POST API like this:

app.post('/test', async (_: any, res: any) => {
  const connection = new Connection('http://localhost:8899', 'confirmed');
  const wallet = Keypair.generate();
  const airdropSignature = await connection.requestAirdrop(
    wallet.publicKey,
    LAMPORTS_PER_SOL
  );
  await connection.confirmTransaction(airdropSignature);
  res.json({ message: 'Ok' });
});

And I'm getting an error "request to http://localhost:8899/ failed, reason: connect ECONNREFUSED". Meanwhile my CLI works. What am I doing wrong? MacOs, node version 18.0.6, @solana/web3.js version 1.55.0


Solution

  • I don't know why, but it worked for me after I changed 'localhost' to '127.0.0.1'