NodeJS: How to get the server's port?
Express 4.x answer:
Express 4.x (per Tien Do's answer below), now treats app.listen() as an asynchronous operation, so listener.address() will only return data inside of app.listen()'s callback:
var app = require('express')();var listener = app.listen(8888, function(){ console.log('Listening on port ' + listener.address().port); //Listening on port 8888});
Express 3 answer:
I think you are looking for this(express specific?):
console.log("Express server listening on port %d", app.address().port)
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.jsalfred@alfred-laptop:~/node$ cat test4/app.js /** * Module dependencies. */var express = require('express');var app = module.exports = express.createServer();// Configurationapp.configure(function(){ app.set('views', __dirname + '/views'); app.use(express.bodyDecoder()); app.use(express.methodOverride()); app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); app.use(app.router); app.use(express.staticProvider(__dirname + '/public'));});app.configure('development', function(){ app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); });app.configure('production', function(){ app.use(express.errorHandler()); });// Routesapp.get('/', function(req, res){ res.render('index.jade', { locals: { title: 'Express' } });});// Only listen on $ node app.jsif (!module.parent) { app.listen(3000); console.log("Express server listening on port %d", app.address().port)}
In express v3.0,
/* No longer valid */var app = express.createServer();app.listen();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);app.get('/', function(req, res) { res.send("Hello World!");});server.listen(3000);console.log('Express server started on port %s', server.address().port);
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
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) { ...}server.listen(8088);console.log(server.address());console.log(server.address().address);console.log(server.address().port);
outputs
{ address: '0.0.0.0', port: 8088 }0.0.0.08088