Ruby 简单Sinatra路由Json文件
我正在编写一个简单的SinatraAPI,它返回特定URL的Json文件 这是server.rb的代码Ruby 简单Sinatra路由Json文件,ruby,json,sinatra,Ruby,Json,Sinatra,我正在编写一个简单的SinatraAPI,它返回特定URL的Json文件 这是server.rb的代码 require 'sinatra' set :public_folder, 'public' get '/' do 'Hello world!' end get '/api/spells' do content_type :json File.read('public/spells.json') end 然而,我一直收到这个错误,没有这样的文件。请帮忙!谢谢大家!
require 'sinatra'
set :public_folder, 'public'
get '/' do
'Hello world!'
end
get '/api/spells' do
content_type :json
File.read('public/spells.json')
end
然而,我一直收到这个错误,没有这样的文件。请帮忙!谢谢大家!
Errno::ENOENT at /api/spells
No such file or directory - /spells.json
如错误消息所示,这是您的路径有问题。根据,默认情况下,
File.read('public/spells.json')
将尝试在名为public/
的文件夹中查找spells.json
文件,确保将json文件放在那里。如何运行应用程序?如果您使用的是机架式或精简型,它们将不会提供来自public/
的文件
您也不需要设置
:public_文件夹“public”
如果文件夹public/
存在,它将自动尝试提供该文件夹中的文件。只有在使用的文件夹不是public/
时,才需要设置该选项