punund punund - 3 months ago 16
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
req
?

UPDATE

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

How about in your main app file:

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

app.use(app.router);

app.get('/', function (req, res, next) {
  console.log(req.socket.server);
});

app.server = http.createServer(app);
app.server.listen(3000);

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.

Comments