What is the difference between app.listen() and http.createServer()?

Answer

app.listen(port) is a convenience method in Express that internally calls http.createServer(app).listen(port) — it creates an HTTP server with the Express app as the request handler and starts listening. It is the simplest way to start an Express server. http.createServer(app) is the explicit Node.js approach. You use it directly when you need access to the raw server instance — for example to set up WebSocket support (socket.io attaches to the raw HTTP server), to create both HTTP and HTTPS servers from the same app, or to configure server-level options. const server = http.createServer(app); server.listen(3000); io.attach(server); is the standard pattern for Express + Socket.io.