我是Ruby和Sinatra的新手,所以我不知道这个问题是客户端还是服务器端。尝试对应用程序执行POST时,我在控制台日志中收到404未找到错误。 '/','/ admin /和'/ connect'页面工作正常,只找不到'/ push'。
Javascript:
$.post( '/arduino/public/push', notification,'json');
config.ru
# encoding: UTF-8
require './stream'
run Sinatra::Application
stream.rb
require 'json'
require 'sinatra'
set :public_folder, Proc.new{File.join(root,"public")}
set server: 'thin'
get '/' do
erb :index
end
get '/admin' do
erb :admin
end
def timestamp
Time.now.strftime("%H:%M:%S")
end
connections = []
notifications = []
get '/connect', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
#out.callback on stream close evt.
out.callback{
#delete the connection
connections.delete(out)
}
end
end
post '/push' do
puts params
#Add the timestamp to the notification
notification = params.merge({'timestamp'=>timestamp}).to_json
notifications.shift if notifications.length > 10
connections.each{ |out| out << "data: #{notification}\n\n"}
end
答案 0 :(得分:1)
你为什么要推进/arduino/public/push
?为什么不只是/arduino/push
(如果/arduino
是您的sinatra应用的根)或仅/push
?
这一行:
set :public_folder, Proc.new{File.join(root,"public")}
表示public
用于静态文件,因此不会触发任何路由。