My Express example ended with app.listen(8000). The FastAPI version ended with uvicorn.run(app, port=8000). Both served the same item at http://127.0.0.1:8000/items/7. The JavaScript version appeared to have one fewer moving part. Did Express do the work of both FastAPI and Uvicorn?
The short answer is that Express's convenience method creates a Node HTTP server and tells it to listen. In the Python example, I start the HTTP server explicitly. To see where each program's job begins, follow that one request: the browser asks for item 7, the server calls my application, and my item handler returns the result. Then consider what happens if the process answering the request exits.12
Interface specifications: how does a server call an application?
Imagine you wrote the server and I wrote the item lookup. You've received the request. Which function should you call? Where will I find the path? How do I hand you the response? An interface specification gives us an agreement we can both implement. HTTP describes the messages exchanged between the browser and server. Once the server has read GET /items/7, a second agreement governs the call from server to application.34
WSGI, the Web Server Gateway Interface, defines a callable of the form application(environ, start_response). My application reads the path from environ["PATH_INFO"], calls start_response with the status and headers, and returns an iterable of byte strings for the body. This WSGI application answers every path with OK.3
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
return [b"OK"]
One request, one response works well for the item page. Suppose the page also has a live chat: over a WebSocket connection, the browser and server can both send messages as they arrive. WSGI defines no event for “the next chat message just arrived.” An application needs a way to receive and send events over the lifetime of the connection.5
My question was “starlette or uvicorn”. The calling convention explains why both appear in one deployment. ASGI, the Asynchronous Server Gateway Interface, specifies a call of the form app(scope, receive, send). For an HTTP request, scope tells the app the method and path. The app can await receive() for the next event, such as part of the request body, and await send(...) to send response headers or body data. If the awaited operation has to wait, the current task can suspend and let another request proceed. Uvicorn implements the server side of ASGI. A Starlette or FastAPI app implements the application side. Hypercorn can call that same app because it implements the same agreement.467
The corresponding handoff in Node is visible in http.createServer(app). When a request arrives, Node calls app(req, res). Express receives the request object and a response object it can write to.21
📓 Note: If you know Java, the Jakarta Servlet specification describes a similar boundary. Tomcat is a container. It calls the servlet's
service(request, response)method. In each case, look for the caller and the parameters through which the application receives the request.89
Execution engines: can I run JavaScript directly with V8?
An interface can tell you the name of a parameter. Something still has to execute the additions, conditionals and return statements inside the function. V8 is the JavaScript engine used by Node.js. CPython is the Python implementation usually behind the python command. V8 and CPython's interpreter are comparable at the level of executing language instructions.10
I asked, “we can use cpython run use python xx.py, why we cannot directly use v8 in command line to run js?” Try a file called sum.js containing just console.log(3 + 4). V8 has a developer shell called d8. With d8 installed, both d8 sum.js and node sum.js can print 7. The addition runs on V8 in both cases. Now replace that line with require("express"). Finding and loading an installed package is another job.1110
Runtimes: who provides the module loader and networking?
Node.js is a runtime. It supplies V8, a module loader, and APIs for files and networking. Node knows how to resolve require("express") and provides the http module Express uses. The d8 shell has its own host environment. A script that relies on Node's package loader and HTTP APIs needs those facilities as well as a JavaScript engine.111021
In Python, python main.py starts the interpreter. My script can import asyncio from the standard library and uvicorn once I've installed it. I could even accept a network connection with asyncio.start_server(). My handler would then receive streams of bytes to read and write. Turning those bytes into a request with method GET and path /items/7 is the HTTP server's job.124
HTTP servers and process management
Who listens on port 8000?
To listen, a program asks the operating system to accept connections at a particular address and port. An HTTP server accepts a connection, reads an HTTP request, calls the application and writes the response back. In Express 4.17.1, app.listen() contains this line: var server = http.createServer(this);. Here this is the Express app. The next line delegates to server.listen(...). Node's built-in http.Server owns the listener. Underneath, libuv works with the operating system to wait for sockets to become ready.1213
Python makes that division visible: app = FastAPI() creates the application, and uvicorn.run(app, port=8000) starts the server. Django's runserver bundles a development server behind one command. In each case it helps to trace the command down to the code accepting connections.1415
This matters for HTTP/2, a version of the network protocol. Node.js 13.6.0 has a separate http2 module, while Express 4's app.listen() calls http.createServer. On the Python side, I can change which HTTP versions the client can negotiate simply by running the same app with a different server.1617
“express has restart feature ?” What about FastAPI?
Run node server.cjs or uvicorn main:app --workers 1. If the entire server process exits, no code inside that process remains to restart it. A development file watcher is a separate mechanism: Node 22's node --watch server.cjs and Uvicorn's --reload restart a service after a file change. In Uvicorn 0.30.6, development reload and multi-worker supervision are separate launch modes.1718
For crash recovery, a supervisor stays alive, starts the processes that handle requests (the workers), and replaces a worker that exits. Uvicorn can do that when started with several workers. PM2 can supervise a Node process. If the supervisor itself exits, recovery requires a service manager outside it. To come back after a machine reboot, that outer service also needs startup configuration. An example of running a Node service under PM2 (in Chinese)1819
What changes when workers are forked or spawned?
Gunicorn 23 uses a pre-fork model. Its master process forks child workers. With --preload the master imports the app first. If all four workers need a large read-only data set, the OS can share memory pages until a process writes to one (copy-on-write). This can save repeated initialization and memory. Open resources can be inherited too. For a database connection pool, a straightforward approach is to open connections in each worker after the fork. On Unix, sending the Gunicorn master the HUP signal also tells it to reread configuration, start replacement workers and let the old ones finish serving requests.1920
Uvicorn 0.30.6 uses spawn. Each worker starts a fresh Python interpreter and imports the app. That model works on Windows and gives each worker its own initialization, with the associated cost if startup is expensive. On Unix its parent also handles HUP, restarting workers one by one. Gunicorn's configuration reread and its ability to combine pre-fork with preloading are the specific features I was looking for in the original comparison. The better startup model depends on the platform and what the workers need to load or safely share.18211922
“compare uvicorn hypercorn and gunicorn”
My starting question was “pm2 to nodejs is like uvicorn to py?” Read each row twice: who handles the network request, and who survives a worker crash? These versions were available in June 2025. The protocol column assumes the client connects directly. A reverse proxy, a server sitting in front of this one, can negotiate a different protocol on its own connection to the browser.23719
| Launch command | HTTP server and application interface | Protocols on the client connection | Process supervisor |
|---|---|---|---|
pm2 start server.cjs for the Express example | Node's http.Server calls Express's app(req, res) | HTTP/1.1 with this app.listen() | PM2 |
uvicorn main:app --workers 4 | Uvicorn 0.30.6 calls ASGI apps natively and includes a deprecated WSGI compatibility mode | HTTP/1.1 and WebSocket for ASGI apps. No HTTP/2 or HTTP/3 listener in this release. | Uvicorn parent |
hypercorn main:app --workers 4 | Hypercorn 0.17.3 serves ASGI and WSGI apps | HTTP/1.1 and HTTP/2, with WebSocket support over both. HTTP/3 requires hypercorn[h3] and a QUIC listener over UDP. | Hypercorn parent |
gunicorn -w 4 flaskapp:app | Gunicorn 23.0.0 default synchronous worker calls a WSGI app | HTTP/1.x. The default worker has no WebSocket, HTTP/2 or HTTP/3 support. | Gunicorn master |
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app | Uvicorn's worker class calls an ASGI app | HTTP/1.1 and WebSocket, as with standalone Uvicorn | Gunicorn master |
So yes: multi-worker Uvicorn alone and Gunicorn alone each cover the two responsibilities of PM2 plus Node's HTTP server. In the combined Gunicorn/Uvicorn deployment, Gunicorn supervises and Uvicorn handles the protocol. The available HTTP versions follow the implementation inside the worker.181924
What about Gunicorn plus Hypercorn? The original answer said “best of both worlds”, but the protocol cell also said “depending on worker”. A worker must actually implement Gunicorn's worker interface. Uvicorn 0.30.6 ships the adapter named in the table. Hypercorn 0.17.3 ships its own launcher and no equivalent Gunicorn worker class. If the requirement is to serve FastAPI directly over HTTP/2, standalone Hypercorn in that release already does it. To serve browsers over HTTPS, configure a certificate and private key.72524
I started two workers in each deployment and used the same local HTTPS certificate for all four. I requested /items/7 with curl 8.5.0, offering HTTP/2 with fallback to HTTP/1.1. To try the Hypercorn case, save the FastAPI app below as main.py, install hypercorn==0.17.3, and provide a certificate valid for the local address. Run the second command in another terminal.
hypercorn main:app --workers 2 --bind 127.0.0.1:8000 --certfile cert.pem --keyfile key.pem
curl --http2 --cacert cert.pem -sS -o /dev/null \
-w 'HTTP/%{http_version} %{http_code}\n' https://127.0.0.1:8000/items/7
All four returned 200. Standalone Hypercorn negotiated HTTP/2. Standalone Uvicorn, Gunicorn's default worker and Gunicorn with Uvicorn workers negotiated HTTP/1.1. The default Gunicorn worker served a WSGI app. The other three served the same FastAPI app. HTTP/3 in the table comes from the release documentation.7
Web frameworks: who sends /items/7 to my function?
An app has many possible handlers: look up an item, log in, place an order. Choosing a handler by HTTP method and path is routing. Express provides routing and response helpers. So does Starlette. Starlette and FastAPI both belong in the web framework layer. Inside that layer, FastAPI builds on Starlette: its class declaration is class FastAPI(Starlette):. I hand just one app to Uvicorn. That app uses Starlette's routing machinery and adds features such as converting the path text to an integer from the annotation item_id: int.62627
I had followed a similar call in Express 5's source. Express 5 delegates routing to the separate router package from pillarjs/router. In its lib/layer.js a Layer holds a handler. The router looks for matching layers in registration order and calls a matching layer's handler. For choosing a handler, this package is comparable to Starlette's routing component. Starlette also works as a web framework in its own right. Express 5's use of that package is a separate example from the Express 4 listener above. Tracing Express 5's router and next() (in Chinese)
Flask and Django also belong in this layer. Flask exposes a WSGI application. Django provides WSGI and ASGI entry points. Starlette and FastAPI expose ASGI applications. An ASGI entry point still has to implement the events it wants to handle. For example, Django 5.2.3's built-in ASGI handler accepts HTTP. To add chat on the same listening port, Django Channels can route HTTP to Django views and WebSocket connections to a consumer, the object whose handler responds to chat messages. Uvicorn handles the network protocol for both.281429306
“is express's server logic async?”
In a directory with Node installed, run npm install express@4.17.1. Save this as server.cjs, run node server.cjs and visit http://127.0.0.1:8000/items/7.
const express = require("express");
const app = express();
app.get("/items/:item_id", (req, res) => {
res.json({ id: req.params.item_id });
});
app.listen(8000, "127.0.0.1");
app.get() registers a route. :item_id matches a path segment, so req.params.item_id contains the string "7". The response is JSON, a text format for structured data. The browser receives {"id":"7"}. An ordinary function is sufficient to handle this request. What happens if a second customer arrives while a report takes half a second to prepare?31
| What the report handler does | Can the same Node process answer the item request? |
|---|---|
await readRows() with an asynchronous database driver | Yes. The report handler suspends while the driver waits, and the event loop can call the item handler. |
Adds up rows in a JavaScript for loop without yielding | The item handler waits for the main JavaScript thread, even if the report handler was declared async. |
Why does the Python example return a number?
Stop Node with Ctrl+C to free port 8000. In a Python 3.12 virtual environment, run python -m pip install fastapi==0.111.1 starlette==0.37.2 uvicorn==0.30.6. Save this as main.py and run python main.py.
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"id": item_id}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Now the same URL returns {"id":7}. Uvicorn calls app according to ASGI. Starlette's routing matches /items/{item_id}. FastAPI uses the Pydantic validation library and the int annotation to convert the path text, then calls read_item(7). Try /items/abc: the route matches, but validation fails with HTTP 422 before the handler runs. An unregistered path gives HTTP 404. Stop Uvicorn altogether and the browser cannot connect to the port. Those are three observable boundaries: validation, routing and listening.276
I also asked “can i use uvicorn programatically”. The last two lines of the Python example do exactly that. For a single worker I can pass the application object directly. When enabling reload or multiple workers, I use an import string such as uvicorn.run("main:app", workers=4) inside the same if __name__ == "__main__" guard. The child processes can then import the application without starting another server on import.18
In my query management project, the JavaScript entry point used app.listen(PORT, …). The Python entry point used uvicorn.run("sqlman.main:app", …), naming the module and the application to import. For the listener, compare Node's HTTP server with Uvicorn. For selecting and calling my item handler, compare Express with Starlette and the FastAPI framework built on it.151
Footnotes
-
Express contributors. (2019). application.js, v4.17.1. GitHub. app.listen, lines 616–619. ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
Node.js contributors. (2020). HTTP. Node.js v13.6.0 Documentation. http.createServer. ↩ ↩2 ↩3 ↩4
-
Eby, P. J. (2010, September 26). PEP 3333 – Python Web Server Gateway Interface v1.0.1. Python Enhancement Proposals. The application/framework side. ↩ ↩2
-
ASGI contributors. (n.d.). ASGI specification, v3.0. Connection scope and events. ↩ ↩2 ↩3
-
ASGI contributors. (n.d.). Introduction. ASGI Documentation. What’s wrong with WSGI?. ↩
-
Starlette contributors. (2024). Starlette, v0.37.2. GitHub. A minimal Starlette application · Request routing. ↩ ↩2 ↩3 ↩4
-
Jones, P., & Hypercorn contributors. (2024). Hypercorn, 0.17.3. PyPI. Interfaces, HTTP/2 and optional HTTP/3. ↩ ↩2 ↩3 ↩4 ↩5
-
Jakarta Servlet Team. (2022, May 12). Jakarta Servlet Specification, v6.0. Eclipse Foundation. §2.1 Request Handling Methods. ↩
-
Apache Software Foundation. (n.d.). Apache Tomcat 10.1 Documentation. Apache Tomcat. Introduction. ↩
-
Node.js contributors. (n.d.). The V8 JavaScript engine. Node.js Learn. The role of V8 in Node.js. ↩ ↩2 ↩3
-
V8 team. (n.d.). Using d8. V8. Running JavaScript from the command line. ↩ ↩2
-
Python Software Foundation. (n.d.). Streams. Python 3.12 Documentation. asyncio.start_server. ↩
-
libuv contributors. (n.d.). Design overview. libuv Documentation. The I/O loop. ↩
-
Django Software Foundation. (n.d.). How to deploy Django. Django 5.2 Documentation. WSGI and ASGI · runserver. ↩ ↩2
-
CaiBorui [S2thend]. (2025, July 2). SqlQueryManagement, commit b088abf. GitHub. Node.js entry point · Python entry point. ↩ ↩2
-
Node.js contributors. (2020). HTTP/2. Node.js v13.6.0 Documentation. Server-side example. ↩
-
Node.js contributors. (2024). Command-line API, v22.12.0. Watch mode. ↩
-
Uvicorn contributors. (2024). Deployment, v0.30.6. Built-in process management and Gunicorn deployment. ↩ ↩2 ↩3 ↩4 ↩5
-
Gunicorn contributors. (2024). Gunicorn, 23.0.0. Master process, worker types and pre-fork · HUP and configuration reload · preload_app. ↩ ↩2 ↩3 ↩4 ↩5
-
SQLAlchemy authors. (2025). Connection pooling, rel_2_0_41. Using Connection Pools with Multiprocessing or os.fork(). ↩
-
Uvicorn contributors. (2024). multiprocess.py, v0.30.6. GitHub. HUP handling and rolling worker restarts. ↩
-
Python Software Foundation. (n.d.). multiprocessing. Python 3.12 Documentation. Contexts and start methods. ↩
-
Uvicorn contributors. (2024). config.py, v0.30.6. Protocol and application interface options. ↩
-
Uvicorn contributors. (2024). workers.py, v0.30.6. Gunicorn worker classes. ↩ ↩2
-
Hypercorn contributors. (2024). Hypercorn, 0.17.3. Package entry points and extras · Worker startup. ↩
-
FastAPI contributors. (2024). applications.py, v0.111.1. GitHub. FastAPI class declaration. ↩
-
FastAPI. (n.d.). Path parameters. FastAPI Documentation. Data conversion and data validation. ↩ ↩2
-
Pallets Projects. (n.d.). Design Decisions in Flask. Flask Documentation. The Explicit Application Object. ↩
-
Django Software Foundation. (2025). ASGIHandler, Django 5.2.3. GitHub. Connection type check in ASGIHandler. ↩
-
Django Channels contributors. (2025). Tutorial part 2: Implement a chat server, Channels 4.2.2. GitHub. Protocol routing and the chat consumer. ↩
-
Node.js contributors. (2025, May 12). Don't block the event loop (or the worker pool). Node.js Learn. How a callback affects other clients. ↩