Application Dispatcher

This middleware creates a single WSGI application that dispatches tomultiple other WSGI applications mounted at different URL paths.

A common example is writing a Single Page Application, where you have abackend API and a frontend written in JavaScript that does the routingin the browser rather than requesting different pages from the server.The frontend is a single HTML and JS file that should be served for anypath besides “/api”.

This example dispatches to an API app under “/api”, an admin appunder “/admin”, and an app that serves frontend files for all otherrequests:

  1. app = DispatcherMiddleware(serve_frontend, {
  2. '/api': api_app,
  3. '/admin': admin_app,
  4. })

In production, you might instead handle this at the HTTP server level,serving files or proxying to application servers based on location. TheAPI and admin apps would each be deployed with a separate WSGI server,and the static files would be served directly by the HTTP server.

  • class werkzeug.middleware.dispatcher.DispatcherMiddleware(app, mounts=None)
  • Combine multiple applications as a single WSGI application.Requests are dispatched to an application based on the path it ismounted under.

Parameters:

  • app – The WSGI application to dispatch to if the requestdoesn’t match a mounted path.
  • mounts – Maps path prefixes to applications for dispatching.