سؤال

I have rails application. If I start it with rails s (port 3000), it works perfectly both on my machine and every device on my local network via the ip address (192.168.0.3 in my case).
I have sinatra application. If I start it with ruby app.rb (port 4567), it works perfectly on my machine, but it it is not accessible from other devices on my local network.

enter image description here

Both application use Thin as an app server.

Is it something related to how sinatra works?

هل كانت مفيدة؟

المحلول

Try ruby app.rb -o 0.0.0.0 or ruby app.rb -e production. Either should work.

نصائح أخرى

Let me add some further info to Ivan's answer. Sinatra's README on command line says:

Sinatra applications can be run directly:

ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s
HANDLER] 

Options are:

-h # help
-p # set the port (default is 4567)
-o # set the host (default is 0.0.0.0)
-e # set the environment (default is development)
-s # specify rack server/handler (default is thin)
-x # turn on the mutex lock (default is off)

Per the document the default HOST is 0.0.0.0, but I still have to specify a "-o 0.0.0.0" just like Ivan said. Otherwise the server cannot be accessed from outside the server machine. How strange!

Just want to add to Ivan's answer and Robert's clarification.

By default, you sinatra runs in development mode, not production mode. In development mode, the default host to which sinatra will bind is 'localhost', meaning only the local machine can interact with it.

Once you specify '-e production' your sinatra webapp is running in production mode, where the default host it binds to is 0.0.0.0, which means that it can interact with all others.

Alternatively, if you want to remain in development, specify '-o 0.0.0.0'

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top