Take the 2-minute tour ×

You often see example hello world code for Node that creates an Http Server, starts listening on a port, then followed by something along the lines of:

console.log('Server is listening on port 8000');

But ideally you'd want this instead:

console.log('Server is listening on port ' + server.port);

How do I retrieve the port the server is currently listening on without storing the number in a variable prior to calling server.listen()?

I've seen this done before but I can't find it in the Node documentation. Maybe it's something specific to express?

share|improve this question

10 Answers 10

up vote 17 down vote accepted

Note: this answer is no longer valid for Express 3.0. See below for Express 3 answer.

I think you are looking for this(express specific?):


You might have seen this(bottom line), when you create directory structure from express command:

alfred@alfred-laptop:~/node$ express test4
   create : test4
   create : test4/app.js
   create : test4/public/images
   create : test4/public/javascripts
   create : test4/logs
   create : test4/pids
   create : test4/public/stylesheets
   create : test4/public/stylesheets/style.less
   create : test4/views/partials
   create : test4/views/layout.jade
   create : test4/views/index.jade
   create : test4/test
   create : test4/test/app.test.js
alfred@alfred-laptop:~/node$ cat test4/app.js 

 * Module dependencies.

var express = require('express');

var app = module.exports = express.createServer();

// Configuration


app.configure('development', function(){

app.configure('production', function(){

// Routes

  res.render('index.jade', {
    locals: {
        title: 'Express'

// Only listen on $ node app.js

if (!module.parent) {
share|improve this answer
Thanks, I think that's exactly what I'm looking for. I'll accept it as soon as I get a chance to test it. Cheers. –  Box9 Jan 30 '11 at 20:54
Your welcome :) –  Alfred Jan 30 '11 at 20:57
@Alfred What about his welcome? ;) –  alex May 12 '13 at 21:24

In express v3.0,

/* No longer valid */
var app = express.createServer();
console.log('Server running on %s', app.address().port);

no longer works! For Express v3.0, you should create an app and a server this way:

var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

    res.send("Hello World!");


I ran in to this issue myself and wanted to document the new syntax. This and other changes in Express v3.0 are visible at https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x

share|improve this answer
Or you could just use the old method of creating the server, which still works. There just seems to no longer be a way to access the port afterword. However, since you are specifying the port yourself in the call to server.listen, there really isn't a need to use server.address().port, since you can just use the same value that you passed into server.listen. –  Mary Hamlin Jul 31 '12 at 16:49
(Although I did just read the migration guide and see that the method for creating an app and server that you mentioned above is actually the new preferred method.) –  Mary Hamlin Jul 31 '12 at 20:43
@MaryHamlin: This is useful if you are passing 0 to server.listen(), in which case a random port number is assigned. You might do this if you’re running several Express apps on one server and you don’t want to manually assign port numbers. –  Nate Aug 31 '12 at 7:28
app.listen() also returns the http server instance. –  Vicary Jan 5 at 9:51

In the current version (v0.5.0-pre) the port seems to be available as a property on the server object, see http://nodejs.org/docs/v0.4.7/api/net.html#server.address

var server = http.createServer(function(req, res) {



{ address: '', port: 8088 }
share|improve this answer

If you're using express, you can get it from the request object:

req.app.settings.port // => 8080 or whatever your app is listening at.
share|improve this answer

Requiring the http module was never necessary.

An additional import of http is not necessary in Express 3 or 4. Assigning the result of listen() is enough.

var server = require('express')();

  res.send("Hello Foo!");

var listener = server.listen(3000);
console.log('Your friendly Express server, listening on port %s', listener.address().port);
// Your friendly Express server, listening on port 3000

Again, this is tested in Express 3.5.1 & 4.0.0. Importing http was never necessary. The listen method return an http server object. https://github.com/visionmedia/express/blob/master/lib/application.js#L531

share|improve this answer

With latest node.js (v0.3.8-pre): I checked the documentation, inspected the server instance returned by http.createServer(), and read the source code of server.listen()...

Sadly, the port is only stored temporarily as a local variable and ends up as an argument in a call to process.binding('net').bind() which is a native method. I did not look further.

It seems that there is no better way than keeping a reference to the port value that you provided to server.listen().

share|improve this answer
+1 and thanks for the research. I'm accepting Alfred's answer since he found the exact thing I was looking for, but I'm glad I know it's not in the Node core now. –  Box9 Jan 31 '11 at 7:29

In case when you need a port at the time of request handling and app is not available, you can use this:

share|improve this answer

The simplest way to convert from the old style to the new (Express 3.x) style is like this:

var server = app.listen(8080);
console.log('Listening on port: ' + server.address().port);

Pre 3.x it works like this:

/* This no longer works */
console.log('Listening on port: ' + app.address().port);
share|improve this answer

The findandbind npm addresses this for express/restify/connect: https://github.com/gyllstromk/node-find-and-bind

share|improve this answer

I was asking myself this question too, then I came Express 4.x guide page to see this sample:

var server = app.listen(3000, function() {
share|improve this answer
But I don't know why server.address().address is always on my local development machine (OSX). –  Tien Do Oct 2 at 6:20

Your Answer


