0

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??

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You must enter api not \api.

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

Comments

0

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!!

1 Comment

Glad to hear and sorry our solutions did not help ):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.