Chapter 12: Running app from physical device gives error "Connection refused"

When running the app on the simulator everything works, but when I try and run the code from my iPhone 11 Pro, I get the following error.

2020-10-10 13:13:00.698589+0200 TILiOS[995:84641] [connection] nw_socket_handle_socket_event [C1.1:2] Socket SO_ERROR [61: Connection refused]

2020-10-10 13:13:00.701632+0200 TILiOS[995:84641] [connection] nw_socket_handle_socket_event [C1.2:2] Socket SO_ERROR [61: Connection refused]

2020-10-10 13:13:00.703308+0200 TILiOS[995:84641] Connection 1: received failure notification

2020-10-10 13:13:00.703373+0200 TILiOS[995:84641] Connection 1: failed to connect 1:61, reason -1

2020-10-10 13:13:00.703404+0200 TILiOS[995:84641] Connection 1: encountered error(1:61)

2020-10-10 13:13:00.711549+0200 TILiOS[995:84674] Task <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])

2020-10-10 13:13:00.717426+0200 TILiOS[995:84674] Task <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.” UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x280bc3180 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 “(null)” UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(

“LocalDataTask <5D3446D3-8916-4F0E-B1C5-91CDE4C67C33>.<1>”

), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost/api/acronyms, NSErrorFailingURLKey=http://localhost/api/acronyms, _kCFStreamErrorDomainKey=1}

I’m guessing this could be because running from my phone “localhost” is not accessible but changing my baseURL to my machines ip: “http://192.168.1.7:8080” does not work. Also tried adding “Allow Local Networking: Yes” in Info.p list but found no joy.

Running:
macOS Catalina v.10.15.7
Xcode: v.12.0.1
Docker: 2.4.0.0

This is not a show stopper but I would really love to figure it out and any help would be appreciated?

Hey so the reason is that by default Vapor binds to localhost which only allows connections from that machine. So when running in the simulator it’s all fine because it’s the same machine. To allow connections from external IP addresses, you need to set the hostname. The easiest way to do this is in configure.swift with:

app.http.server.configuration.hostname = "0.0.0.0"