Express app.listen() Method

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 4 Code Examples
Express.js

What you’ll learn

  • How to start an Express server with app.listen().
  • How to configure port and host for local and production environments.
  • How to use startup callbacks and basic error handling.
  • How to avoid common server start issues like port conflicts.

Overview

app.listen() starts your HTTP server so incoming requests can reach Express routes and middleware.

Server startup

Binds app to a port and begins accepting HTTP connections.

Port flexibility

Use fixed dev ports or dynamic ports with process.env.PORT.

Operational readiness

Add startup logs and error handling for smooth deployment behavior.

Syntax

javascript
app.listen(port, callback)
app.listen(port, host, callback)
  • port: network port number, for example 3000.
  • host: optional interface, such as 127.0.0.1 or 0.0.0.0.
  • callback: runs after successful server startup.
1

Basic listen example

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

app.get('/', function (req, res) {
  res.send('Server is running');
});

app.listen(3000, function () {
  console.log('Server started on http://localhost:3000');
});
2

Environment-based port setup

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

const port = process.env.PORT || 3000;
const host = process.env.HOST || '0.0.0.0';

app.listen(port, host, function () {
  console.log('Listening on ' + host + ':' + port);
});

📋 app.listen() vs server.listen()

ApproachUse caseExample
app.listen()Simple Express startupapp.listen(3000)
http.createServer(app).listen()Advanced server controlserver.listen(3000)

🧪 Testing checklist

  • Confirm app starts and logs expected startup message.
  • Open browser or API client and verify routes respond correctly.
  • Test with custom PORT env value to verify configuration.
  • Simulate a used port and confirm error behavior is handled.

Pitfalls to avoid

Hardcoded port only

Deployment failures

Use process.env.PORT fallback for platform compatibility.

Ignoring listen errors

Silent startup issues

Handle server errors such as EADDRINUSE during startup.

Wrong host binding

Unreachable service

Choose host binding deliberately depending on local vs containerized execution.

❓ FAQ

It starts an HTTP server and binds your Express app to a port (and optionally host).
It runs after the server starts successfully and is commonly used for startup logs.
Yes. You can pass host explicitly, for example app.listen(3000, '0.0.0.0').
Use process.env.PORT with a fallback, such as const port = process.env.PORT || 3000.
The server emits an error (EADDRINUSE). Handle errors and choose another port.

Summary

  • Purpose: app.listen() starts the Express HTTP server.
  • Configuration: use environment-driven port and optional host binding.
  • Reliability: log startup clearly and handle port-related errors.
Did you know?

app.listen() is a convenience wrapper around Node.js http.Server.listen() and starts your Express app server.

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.

4 people found this page helpful