Search code examples
pythonservertornado

Serve two different static files on different endpoints


I have a simple tornado server that shows an index.html by default - this works. However I also want to render a json file for the localhost/default end point and when I go to localhost:8000/default it throws an error (TypeError: get() missing 1 required positional argument: 'path').

Here is the application configuration.

   application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/login", LoginHandler),
    (r"/getToken", TokenHandler),
    (r"/default", tornado.web.StaticFileHandler, {"path": root, "default_filename": "test.json"}),
    (r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"}),
   ])  

Solution

  • The documentation states that:

    Note that a capture group in the regex is required to parse the value for the path argument to the get() method

    What it means is that you need to define a regex group in your url to capture the requested path.

    Example:

    (r"/default/(.*)", ...)