过滤前的 Sinatra

Sinatra before filter

我正在使用 Sinatra,但我不知道如何处理我的问题:

我想 "send" 在他试图走错路时卷曲一条自定义消息。

卷曲http://localhost:port/blabla 这是错误 404,但我想向他发送类似 'error try other path'

的内容

我试过这个:

before'/*' do
  if (params[:splat].to_s =~ /path_i_want/) != 2
    'wrong path'
  end
end

raise 404 但它不起作用。

你能帮帮我吗?

此致。

Sinatra 有一个内置的 404 处理程序,请参阅 Error Handling 页面。你可以在那里做你所有的逻辑。

Not Found

When a Sinatra::NotFound exception is raised, or the response’s status code is 404, the not_found handler is invoked:

not_found do
  'This is nowhere to be found.'
end