简体   繁体   English

如何在PUMA RACK APP中获得所有请求?

[英]How do I get all requests in puma rack app?

I'm writing a dead-simple puma server, and I keep getting 404 for every request. 我正在编写一台简陋的puma服务器,并且每次请求我都会得到404。 How do I configure the '/' path without Sinatra? 如何在没有 Sinatra的情况下配置“ /”路径?

I simply want to catch all requests. 我只是想捕获所有请求。

#config.ru
module Moon
  class HelloWorldApp
    def call(env)
      [200, {}, 'Hello World']
    end
    def each(env=nil)
      env
    end
  end
end

run Rack::Cascade.new Moon::HelloWorldApp.new

I run it like this: 我这样运行:

$ puma config.ru -p 9595

Use this curl: 使用此卷曲:

$ curl http://0.0.0.0:9595/test

And get 404: 并获取404:

127.0.0.1 - - [07/Apr/2015:22:49:25 +0300] "GET /test HTTP/1.1" 404 - 0.0002

Rack::Cascade expects an explicit array of apps to be passed to the constructor (or at least something like an enumerable that responds to each and yields the apps). Rack::Cascade期望的应用程序的明确阵列要被传递给构造(或至少类似的东西来响应每个,并产生所述应用程序可枚举)。 It then calls each on this array to get each app (it looks like you've hit the no method error and tried to work round it by adding an each method to your app). 然后,它在此数组上调用each以获取每个应用程序(看起来您遇到了no method错误,并尝试通过向您的应用程序中添加一个each方法来解决该问题)。

Fix it by changing the run line to: run行更改为:

run Rack::Cascade.new [Moon::HelloWorldApp.new]

You will also need to change the body part of your returned array, it needs to be something that responds to each and yields String s , the simplest way to fix that is to return an array: 您还需要更改返回数组的主体部分,它需要each数组都做出响应并产生String ,这是修复数组的最简单方法是返回数组:

[200, {}, ['Hello World']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM