python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.14k stars 333 forks source link

root page become 404 not found #526

Closed fly50789 closed 1 year ago

fly50789 commented 1 year ago

I cant see root(eq.'/') page of blueprint unless I remove Api. But no matter what I can see '/other' page Is any suggestion?

server_bp = Blueprint('server_bp', __name__,
                       template_folder='templates',
                       static_folder='static',
                       static_url_path='static',
                       )

api = Api(server_bp , version=g.__version__,
              title='CPE SERVER API',
              description='A CPE SERVER API',
              doc='/api/v1/doc/',
              )

@server_bp.route('/')
def index():
    return "root page"

@server_bp.route('/other')
def index2():
    return "other page"
peter-doggart commented 1 year ago

@fly50789 This is a dupe of #523

Due to how flask-restx builds paths inside api, if root (/) is not already registered when you create your API, it creates it either to serve the doc or simply returns a 404 if you specify the doc path as in your example. Unfortunately, there is no easy fix to change the flask-restx internal behaviour because of the flexibility flask gives users in registering endpoints.

To fix, simply define the / route before you create your API object (move it above API in your code above) and it should work as expected.

It's on my to-do list to update the docs to make this clearer!

fly50789 commented 1 year ago

Thanks for your help~