punund punund - 1 year ago 135
Node.js Question

access to HTTP server object in node.js express

I am inside a middleware (

function(req, res, next) {...}

Is there a way to access the HTTP server object from the


Let me be more specific. I am trying to find out a port that the server listens on, or unix socket path, if it's listening on that.

Answer Source

How about in your main app file:

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


app.get('/', function (req, res, next) {

app.server = http.createServer(app);

As Brad mentioned, Express does expose something resembling the object returned from #createServer(), however, TJ has been giving serious consideration to dropping any inclusion of the HTTP module in express in future releases. Using the code above will be future safe.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download