Search code examples
iosnode.jsexpressalamofire

How to access localhost from device


I'm running a node.js Express server on localhost. I'm making some API calls to the server using Alamofire from an iPhone app I'm developing in Swift:

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse) in ... }

url is "http://localhost:5000/api/test"

Everything works fine when I run the app in the simulator; it can access localhost just fine. But when I run on a device, I get "Could not connect to the server."

Do I have to deploy the server code somewhere like Heroku in order to test on a local device connected to the same network?


Solution

  • If you are on the same wifi network, you could use your computer ip address. Otherwise, if you want to be able to access your node app from every wifi network, you could use Ngrok.

    It will expose your address on a public URL