maryj
maryj

Reputation: 111

Can't get request when config routes.rb file with Sinatra

My app structure like this

Gemfile
app.rb
config.ru
lib/routes.rb


# app.rb
require 'sinatra'
class Todo < Sinatra::Base
  set :environment, ENV['RACK_ENV']

  Dir[File.join(File.dirname(__FILE__), 'lib', '*.rb')].each {|lib| load lib}
end

#config.ru
require 'sinatra'
require 'bundler/setup'
Bundler.require

ENV['RACK_ENV'] = 'development'

require File.join(File.dirname(__FILE__), 'app.rb')

Todo.start!

#lib/routes.rb
get '/' do
  "Hello world"
end

When I run with ruby config.rb then locate to localhost:4567, it doesn't recognize the route /. But if I move the code get '/' do into class Todo it works.

Anyone can explain that for me?

Upvotes: 0

Views: 60

Answers (1)

Kashyap
Kashyap

Reputation: 4796

If you have a config.ru file, that means that the application has to be invoked by the rackup utility and not ruby config.ru. The reason for this is that rackup sets a lot of settings before invoking the code inside config.ru. Rackup utility is part of Rack gem that Sinatra builds on, so it will be present if you install Sinatra.

Although not a problem for the example you provided, in a modular style application, you need to require sinatra/base rather than just sinatra.

Upvotes: 1

Related Questions