Comparing flask vs node is a cliche which is formally not correct because:
- There is a python interpreter which is used to run applications built with flask web server framework.
- There is a nodejs interpreter which is used to run an application built with express web server framework (or another one, when
expressis just most popular).
So both flask and express are web frameworks which main aim is:
- Bind on some TCP port e.g. 8080 and wait for incoming connection
- When some HTTP client connects to the port they just handle HTTP requests (which are basic text portions of data kinda
GET /myurl/) by parsing all fields (METHOD, URL, HEADERS) then match URL to some high-level routing configuration (easy for developer) and execute so-called view functions
- View-function again is easy to use for a developer - web framework can easily parse params from the route (e.g. get
1232as separate function argument from URL
/readpost/1232/) ad pass them to view as an additional arguments
- Developer return some text-based or JSON-based object from view function when framework wraps it all into correct HTTP response and sends it back to TCP connection
express are full-featured great tools which can do this job.
What about differences:
Benefit of express and node - it is truly asynchronous
- Nodejs world is truly asynchronous by default. You can run one thread of your express node process and it will handle thousands of requests per second because when one request started to be processed, some asynchronous operation (e.g. read to the database) will release an event loop for handling the new connection.
- In python world flask is blocking (as well as Django), so if you will run a flask server in one process it will be limited to only one request at a time. If user runs a 3-seconds request which is waiting for a slow mail-send server, you will not able to handle another request those 3 seconds. The common solution - using some multi-process/multi-threading runner e.g.
gunicornwhich will spawn threads and you will be handling several requests at a time (worthen then async CPU/memory resources usage of course). Or use
fastapiwhich are async
flaskanalogs. But coding on those libraries with
async/awaitwill require some skill (when a node does this asynchronously be default)
So when you try to make decision who is better flask vs node you can be sure that node will win in terms of efficency.
Benefits of python flask - packages
PyPI has much more cool libraries than
nodejs world. Greatest example - automatic migration system. For flask you can use
SQLAlchemy ORM - you define models as classes, then run alembic and it generates migrations that create tables in the database for you. You rename field inside of model class, again run alembic command and it creates migration which will be easily applied to your college computers or on the server (Same and even better migrator is only shipped in Django). It is hard to find something like this in the nodejs world.
If you don't need relational (SQL) databases - automatic migration is not your priority - probably you don't need schema at all. But there are a lot of cool libraries in the python world, so we tend to think that python development process will be faster and easier.
Who would win in fight flask vs node
From a business point of view in flask vs node battle, Flask, for sure, is more efficient in terms of spending time because of the good packages with good documentation. Faster and simpler development is also easier support. Though with true asynchronicity you can use computing resources more efficiently, most of the projects will have a budget that will win much more from development time benefit.