0

我对 Ruby 和 Sinatra 很陌生,所以我不知道这个问题是客户端还是服务器端。尝试对应用程序进行 POST 时,我在控制台日志中收到 404 not found 错误。'/' 、 '/admin/ 和 '/connect' 页面工作得很好,只有 '/push' 没有找到。

Javascript:

$.post( '/arduino/public/push', notification,'json'); 

配置.ru

# encoding: UTF-8
require './stream'
run Sinatra::Application

流.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
4

1 回答 1

1

你为什么要推/arduino/public/push?为什么不只是/arduino/push(如果/arduino是您的 sinatra 应用程序的根目录)或只是/push

这一行:

set :public_folder, Proc.new{File.join(root,"public")}

建议这public是针对静态文件的,因此不会触发任何路由。

于 2013-06-28T20:52:22.220 回答