-1

I have setup several VMs provisioned through vagrant, each with their own private network IP. The issue comes up is that for one of the VMs, which I have named netcap_backend, I have set to start a nestjs server but when I try to hit an endpoint on my host computer, it returns with a ECONNREFUSED. I have a simple GET endpoint set directly at 192.168.56.101:3000/. My host computer is running Arch, and this is the provisioning for that specific VM:

  config.vm.define "netcap_backend" do |back|
    back.vm.hostname = "netcap-backend"
    
    back.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", env['MEM_BACKEND']] 
      vb.customize ["modifyvm", :id, "--cpus", env['CPU_BACKEND']]
    end
  
    back.vm.network "private_network", ip: env['BACKEND_IP'] # 192.165.56.101
    back.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "#{env['PROVISION_PATH']}/prov_netcap_back.sh"

  end

And this is what I have for my main.ts in my nest server:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    allowedHeaders: '*',
    origin: '*',
    credentials: true,
  });

  await app.listen(3000);
}
bootstrap();

What is strange is that this is configured identically to my frontend VM, and that is running a React which my host computer has no problem connecting to, be it with a different IP.

At first when I tried a simple fetch on firefox, I expected the fetch call to return me with a simple Hello World but, it returned me with a CORS issue, hence the additional enableCors() call in my main.ts. But that didn't seem to solve the issue. I then used Postman to ping my route, that's when I saw the ECONNREFUSED. What I then tried is to see if my host computer can actually talk to the VM, which it could as ping 192.168.56.101 executed just fine with no network errors.

A little update: Tried the same setup but on windows and worked perfectly...

1
  • Ok so I solved it. For those who encounter similar issues, at least for me, both my frontend server and my backend server was trying to expose the server on the same port. I'm not exactly sure why this setup works on Windows but my best guess is that there exists some sort of port conflict resolution. All I had to do was change one of the ports to something other than 3000 and restarted the vagrant virtual machine and it worked. Commented Jun 25, 2023 at 20:46

1 Answer 1

0

The error that is happening here is that while both my frontend and backend server are using different IPs, they have overlapping ports (both using 3000). On my system at least, this caused only one of the servers to be connected to the host-only network. Solution is to use different port numbers for each server that needs to be exposed to the network and restart each vagrant vm that is running the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.