flaskwsgimiddleware

Flask Middleware to be Express-js like


I wonder if it's possible to attach a middleware/function for a specific route like express js. http://expressjs.com/en/guide/using-middleware.html#middleware.application

In my understanding, it seems that flask(WSGI) middleware applies to all request.


Solution

  • The equivalent pattern is a route decorator. Flask has some docs about it. It is pretty flexible, you can validate anything in HTTP request and change the response based on that.

    For example, Flask-Login has the @login_required decorator to add a user authentication step before the route is called.