带有React客户端和RESTFULL API的烧瓶网络服务器

发布于 2025-02-01 04:10:49 字数 560 浏览 3 评论 0原文

你好吗?

我正在尝试在同一烧瓶Web服务器上维护两个服务:一个React客户端(在“/中”可用)和RESTFULL API(可在“/api/”中使用)。

但是,所有路线均针对客户端,我无法将蓝图注册到“/api”。

@app.route('/', defaults={'path': ""})
@app.route('/<path:path>')
def client_route(path):
    return 'Client'


app.register_blueprint(routes, url_prefix="/api")
""" Registro de rotas da aplicação """

printscreen code

我需要以“/api”开头来调用API路由,而所有其他所有其他路由路由(*)呼叫客户端react。

有人可以帮我吗?

How are you?

I'm trying to maintain two services on the same flask web server: A React client (available in "/") and a RestFull API (available in "/api/").

However, all routes are directed to the client and I cannot register the Blueprint to "/api".

@app.route('/', defaults={'path': ""})
@app.route('/<path:path>')
def client_route(path):
    return 'Client'


app.register_blueprint(routes, url_prefix="/api")
""" Registro de rotas da aplicação """

printscreen code

I need routes starting with "/api" to call the API routes, while all other routes (*) call Client React.

Can anybody help me??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卸妝后依然美 2025-02-08 04:10:49

main_view.py

bp = blueprint('main',名称,url_prefix ='/api')

app.py

app.register_blueprint(main_view.bp)

设置蓝色打印时,输入' /api'而不是'prefix = /'节。哦,您已经这样设置了,但是就我而言,我在上面写了。

我希望这会有所帮助。

main_view.py

bp = Blueprint('main', name, url_prefix='/api')

app.py

app.register_blueprint(main_view.bp)

When setting the blue print, enter '/api' instead of 'prefix = /' section. oh You've already set it up like that, but in my case I write above .

I hope this helps.

昔日梦未散 2025-02-08 04:10:49

您必须输入api不是\ api

这是蓝图如何工作的链接: https://realpython.com/flaston.com/flask-blask-blask-bluask-bluask-bluask-bluask-blueprint/

You must enter api not \api.

Here is a link on how blueprints work: https://realpython.com/flask-blueprint/

淡看悲欢离合 2025-02-08 04:10:49

我找到了解决方案!

您只需要交换寄存器的顺序,例如:

app.register_blueprint(routes, url_prefix="/api")
""" Registro de rotas da aplicação """


@app.route('/', defaults={'path': ""})
@app.route('/<path:path>')
def client_route(path):
    if path != "" and os.path.exists(app.static_folder + '/' + path):
        return send_from_directory(app.static_folder, path)
    else:
        return send_from_directory(app.static_folder, 'index.html')

(*),您可以使用此React代码正确使用WebApp。

Thaks!

I found the solution!

You just need to swap the order of the register, like that:

app.register_blueprint(routes, url_prefix="/api")
""" Registro de rotas da aplicação """


@app.route('/', defaults={'path': ""})
@app.route('/<path:path>')
def client_route(path):
    if path != "" and os.path.exists(app.static_folder + '/' + path):
        return send_from_directory(app.static_folder, path)
    else:
        return send_from_directory(app.static_folder, 'index.html')

(*) And you may use this React code to serve the webapp correctly.

Thaks!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文