gpt4 book ai didi

python - 在 Falcon 框架中将所有路径路由到一个处理函数

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:16 26 4
gpt4 key购买 nike

我想将所有以“/api”之类的内容开头的路径路由到相同的处理函数。

如:

/api/foo
/api/bar
/api/foo/bar
/api/bar/baz/this/that

所有的东西都应该用一个函数来处理,我应该能够在/api 之后得到完整的路径。

此功能非常方便,我在 Node.js Express 框架中经常使用它。现在我正在寻找使用 Python Falcon 框架完成同样事情的方法。

更多信息可以在这里找到;它将此功能定义为“列入白名单的“全局”功能。”

http://expressjs.com/api.html#app.all

最佳答案

也许您正在寻找 Falcon 的水槽设施,例如:

class Sink(object):
def on_get(self, req, resp):
resp.body = ('\nTwo things awe me most, the starry sky '
'above me and the moral law within me.\n'
'\n'
' ~ Immanuel Kant\n\n')

app = falcon.API()
handler = Sink().on_get
app.add_sink(handler, prefix='/')

这会将所有 URL 路由到接收器处理程序。

关于python - 在 Falcon 框架中将所有路径路由到一个处理函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33515318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com