I'm having trouble learning how to use Sinatra.
Versions are
Mac OSX 10.11.2
Ruby 2.2.2
Sinatra 1.4.6
I wrote the same code as the first code in this site.
# main.rb
require 'sinatra'
get '/' do
'Hello world!'
end
Then I tried a command in the same derectory
% ruby main.rb
== Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Then I accessed http://localhost:4567 with Google Chrome, but the browser didn't recieve any data (ERR_EMPTY_RESPONSE). Also nothing logged in Terminal.
I tried other port (like 3000) by -p option, but it didn't work.
What am I doing wrong?
I updated gem and reinstalled sinatra, then it worked.
gem update --system
gem uninstall sinatra
gem install sinatra
Related
I'm building a basic Ruby app with Sinatra and SendGrid.
When I try and run the file ruby app.rb the site does not load. When I remove require 'sendgrid-ruby' the app loads. Code below.
app.rb
require "sendgrid-ruby"
require "sinatra"
set :app_file, __FILE__
get '/' do
puts "hi"
end
Gemfile
gem 'sinatra'
gem 'sendgrid-ruby'
Any help? I don't have a config.ru or anything else at this point. No error logging. It just hangs at waiting for LocalHost.
== Sinatra (v1.4.7) has taken the stage on 4567
for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Ruby version: ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15]
EDIT:
Previously, I had require "sinatra" first and I didn't have set :app_file, __FILE__ in there at all.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i'm using sinatra to work with ruby , when i am run the file in the terminal the normal port for sinatra is "4567" , but for three days ago the terminal print this
Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-09-06 00:15:16] INFO WEBrick::HTTPServer#start: pid=770 port=4567
also the webrick port is "4567"
It's difficult to understand the problem you're seeing. Maybe this will help:
If I create a simple test server containing the sample Sinatra code:
require 'sinatra'
get '/hi' do
"Hello World!"
end
And save that to my Desktop as test.rb, I can run Sinatra using:
ruby test.rb
Sinatra tells me:
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
If I uninstall Thin:
gem uninstall thin
Remove executables:
thin
in addition to the gem? [Yn] y
Removing thin
Successfully uninstalled thin-1.6.2
And run the code again:
ruby test.rb
[2014-09-05 14:41:23] INFO WEBrick 1.3.1
[2014-09-05 14:41:23] INFO ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-09-05 14:41:23] INFO WEBrick::HTTPServer#start: pid=26786 port=4567
In either case, the code does what it's supposed to. I can connect using curl and get "Hello World!" back:
curl http://localhost:4567/hi
Hello World!
Sinatra uses Webrick by default, but will pick other servers over it:
gem install puma
Fetching: puma-2.9.1.gem (100%)
Building native extensions. This could take a while...
Successfully installed puma-2.9.1
Parsing documentation for puma-2.9.1
Installing ri documentation for puma-2.9.1
Done installing documentation for puma after 3 seconds
1 gem installed
ruby test.rb
Puma 2.9.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Puma
gem install thin
Fetching: thin-1.6.2.gem (100%)
Building native extensions. This could take a while...
Successfully installed thin-1.6.2
Parsing documentation for thin-1.6.2
Installing ri documentation for thin-1.6.2
Done installing documentation for thin after 0 seconds
1 gem installed
ruby test.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
curl http://localhost:4567/hi
Hello World!
Use -s to control which server Sinatra uses:
ruby test.rb -s puma
Puma 2.9.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Puma
Sinatra Docs:
:run - enable/disable the built-in web server Boolean specifying
whether the built-in web server is started after the app is fully
loaded. By default, this setting is enabled only when the :app_file
matches $0, i.e. when running a Sinatra app file directly with ruby
myapp.rb. To disable the built-in web server:
set :run, false
:server - handler used for built-in web server. String
or Array of Rack server handler names. When the :run setting is
enabled, Sinatra will run through the list and start a server with the
first available handler. The :server setting is set as follows by
default:
set :server, %w[thin mongrel webrick]
Sinatra README:
Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort:
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
Install the gem:
gem install sinatra
And run with:
ruby myapp.rb
View at: http://localhost:4567
It is recommended to also run gem install thin, which Sinatra will pick up if available.
By default, Sinatra first looks to see if you have the Thin server software installed--if yes, Sinatra uses Thin to respond to requests on port 4567. If you do not have Thin installed, next Sinatra checks if you have the Mongrel server software installed, and Sinatra uses Mongrel if it is installed, and finally Sinatra uses the always available Webrick server software, which comes with ruby.
The message you are seeing is Sinatra telling you which server software it is using to respond to requests on port 4567.
I have my web.rb where I have
require 'rubygems'
require 'sinatra'
require "sinatra/reloader"
Then I start my web application by double click on the web.rb short cut. After any change in web.rb I have to exit the sinatra and run it again.
I thought that "sinatra/reloader" would help me not to manual reaload.
configuration (MS Windows)
ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
sinatra (1.2.6, 1.0)
sinatra-advanced-routes (0.5.1)
sinatra-reloader (0.5.0)
sinatra-sugar (0.5.1, 0.5.0)
thin (1.2.7 x86-mswin32)
register Sinatra::Reloader
I changed to rack-reloader months back, realized it didn't work well even with hacks
switched back and forgot to use register
the drawback of sinatra sometimes is the slightly more wisdom-of-crowds documentation
if I start my web application from dos prompt via "ruby web.rb" everything works fine
if I start my web application
via double click web.rb or
from batch file
the application starts but sinatra-reload doesn't work
#nash thank you for your help
When I modify the code and have to restart server to see results. Have any way out?
There are a few options, detailed in the Sinatra FAQ. The simplest appears to be to use shotgun, which you can install and invoke as follows:
$ sudo gem install shotgun
$ shotgun myapp.rb
or if you use define your app by inheriting from Sinatra::Base and use a config.ru file:
$ shotgun config.ru -p 4567
If you use Phusion Passenger, you can put this file in the application’s root folder
tmp/always_restart.txt
and it will restart on every request.
http://www.modrails.com/documentation/Users%20guide%20Apache.html ( section 8.7 )
Better way is to use reloader from sinatra-contrib gem (also from Sinatra FAQ): First install sinatra-contrib gem, then ensure your application .rb file starts with these lines:
require 'sinatra'
require 'sinatra/reloader' if development?
And then any modified config files will be reloaded (no need to restart server!)
I'd like to start a Sinatra app from Mongrel on Windows, rather than Sinatra starting Mongrel in the background.
Is there a simple way to use Mongrel for Sinatra? It's looking for a rails app by default.
Edit: Suggested solution is to simply run a VMWare or SunBox with real Linux and deal with the corporate issues that way.
Check out http://sinatra-book.gittr.com/.
FastCGI is the chapter you're looking for within the page, because that's what mongrel is
if you have a rake config, like config.ru
then install thin gem
then
thin -p 3000 -r config.ru
I think.
-r