Express.js app.listen() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

app.listen() starts an HTTP server for your Express app and binds it to a TCP port (or a UNIX socket). It is identical to Node.js http.Server.listen() and returns the http.Server instance.

Call it after you register routes with app.get() and other methods. The Express hello-world app listens on port 3000 and logs when ready.

Syntax

Call app.listen in your Express.js app. Start with this line.

Express
app.listen([port[, host[, backlog]]][, callback])
app.listen(path[, backlog][, callback])
CallWhat it does
app.listen([port[, host[, backlog]]][, callback])Primary signature.
app.listen(path[, backlog][, callback])Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

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

Parameters

ParameterDescription
portRequired. TCP port. If omitted or 0, the OS assigns an unused port.
hostRequired. Interface to bind — for example '127.0.0.1' or '0.0.0.0'.
backlogRequired. Max length of the pending connections queue (advanced).
pathRequired. UNIX socket path instead of host/port (for example '/tmp/sock').
callbackRequired. Called when listening (or with an error on bind failure in Express 5).

Return Value

app.listen() returns an http.Server (Node’s HTTP server). Keep that reference when you need close(), address info, or HTTPS upgrades — the return value is not app.

Express
const server = app.listen(3000, () => {
  console.log('listening on', server.address());
});

// Later (tests / graceful shutdown):
// server.close();

Express.js Examples

Example 1 — Hello World Listen

Official starter pattern: route first, then listen with a ready log.

Express
const express = require('express');
const app = express();
const port = 3000;

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

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

How It Works

  1. Create the app. express() returns the request handler.
  2. Register a route. app.get('/') answers the homepage.
  3. Bind port 3000. app.listen starts the HTTP server.
  4. Log when ready. The callback runs after the server is listening.

Example 2 — PORT from Environment

Hosts like Heroku set PORT. Fall back locally to 3000.

Express
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

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

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

How It Works

  1. Read PORT. Use the environment value when present.
  2. Fallback. Locally, || 3000 keeps a predictable default.
  3. Keep the server. app.listen returns http.Server.
  4. Inspect the bind. server.address() shows the real host/port (handy with port 0 too).

Example 3 — Handle Bind Errors

Express 5 passes listen failures into the callback — check before celebrating.

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

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

const server = app.listen(8080, '0.0.0.0', (error) => {
  if (error) {
    console.error('Listen failed:', error.message);
    process.exit(1);
  }
  console.log(`Listening on ${JSON.stringify(server.address())}`);
});

How It Works

  1. Bind host and port. 0.0.0.0 accepts connections on all IPv4 interfaces.
  2. Check error. On failure (busy port), log and exit instead of printing a false ready message.
  3. Success path. When error is absent, log server.address().
  4. Why it matters. Without this check, a failed bind can look like a successful start.

Runtime Support

app.listen() is an Express.js application method built on Node’s HTTP server. Express 5 needs Node.js 18+. Browsers only connect to the listening host/port.

✓ Server · Express 4 & 5

app.listen()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.listen() Express on Node.js

Bottom line: Run examples with Node.js and the express package, then open http://localhost:<port>/ in a browser.

Frequently Asked Questions

It creates an HTTP server for your Express app and starts listening for connections on a port (or a UNIX socket). It is the same as Node’s http.Server.listen().
It runs when the server is ready. In Express 5, if listen fails (for example EADDRINUSE), the error is passed to that callback as its argument.
Use process.env.PORT with a local fallback, for example const port = process.env.PORT || 3000.
The operating system assigns an unused port. Useful for tests. Read the chosen port from server.address().
No. It starts your Node.js server. Browsers only connect to the host and port you bound.

Next: app.locals

Share application-level variables with your templates.

app.locals tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful