What is the Express.js application lifecycle?

Answer

Understanding the Express application lifecycle helps diagnose bugs. Startup: app.js executes — middleware is registered, routes are defined, database connections are established. Listening: app.listen() creates an HTTP server and starts the event loop waiting for connections. Request handling: for each request, Express traverses the middleware stack in order — each middleware calls next() or sends a response. If no route matches, the request falls through to a 404 handler. If a middleware calls next(err), error-handling middleware takes over. Response: once res.send() or similar is called, the response is sent. The connection may be kept alive (HTTP/1.1 keep-alive) for subsequent requests. Shutdown: server.close() stops new connections; process exits after in-flight requests complete.