Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

app.listen

Starts an Express application listening for incoming HTTP connections. It is a convenience wrapper around Node.js HTTP server listening and returns the underlying server.

How it works

app.listen() creates an HTTP server for the Express application and delegates listening to Node.js with the supplied arguments. It starts accepting connections on the selected port or host and returns the underlying server object, which can be monitored or controlled with Node.js APIs.

Syntax

app.listen([port[, host[, backlog]]][, callback])

Parameters

NameTypeDescription
...argsNode.js listen argumentsArguments accepted by Node.js Server.listen(), commonly a port and optional callback; a host may also be supplied.

Returns

A Node.js HTTP server returned by the underlying server.listen() call.

Key points

  • Starts the Express application listening for incoming connections using the supplied host and port.
  • Its listening behavior follows Node.js http.Server.listen(), including the supported listening argument forms.
  • The Express application itself is a callable function that can serve as a Node.js request handler.
  • You can pass the app directly to a Node HTTP server constructor when you need explicit server creation.
  • The same Express request-handling code can be reused with separately created HTTP and HTTPS servers.
  • Internally, app.listen() creates an HTTP server using the Express application as the handler.

Common mistakes

  • Use app.listen() for the normal HTTP case; use http.createServer(app) or https.createServer(options, app) when you need a specific server type such as HTTPS.

Example: Start an Express HTTP server

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello world');
});

const server = app.listen(3000, () => {
  console.log('Listening on port 3000');
});

server.on('error', console.error);

Creates an Express app, registers a route, and starts listening for HTTP connections on port 3000. The returned server can be used with Node.js server APIs.

When to use it

Start an HTTP service

Run a conventional Express web application on a TCP port.

Manage the returned server

Retain access to Node.js server events and controls after startup.

Share request handling across protocols

Reuse the same Express application with separately created HTTP and HTTPS servers.

Interview questions

QWhat does app.listen() do internally?

app.listen() is Express's convenience wrapper around Node's HTTP server. It creates a server with the Express application as its request handler and invokes the server's listen method with the provided arguments.

Follow-up: How would you serve HTTPS? Create an HTTPS server explicitly with https.createServer(options, app), then call listen on that server.

QWhat are the main inputs and result of app.listen()?

The port argument identifies where the application accepts connections, such as 3000. The optional callback runs after the server begins listening, while the returned server exposes Node.js server functionality.

Follow-up: How can startup failures be handled? Attach an error listener to the returned server and handle events such as a port already being in use.

QWhy can an Express app be passed to Node's server constructors?

An Express application is callable as a request handler, so Node's HTTP server can dispatch incoming requests to it. This lets the same application be passed directly to HTTP or HTTPS server constructors.

Follow-up: Why avoid app.listen() for HTTPS? app.listen() constructs an HTTP server, so HTTPS requires explicitly constructing the HTTPS server with TLS options.

  • express()
  • app.use()
  • app.get()
  • http.createServer()
  • https.createServer()

Source: 4.x API Reference · Express.js. Checked 2026-10-15.