访问node.js中的HTTP服务器对象表达式 [英] access to HTTP server object in node.js express
问题描述
函数(req,res,next){...}
)。 有没有办法从 req
访问HTTP服务器对象?
更新
让我更具体。我正在尝试找出服务器侦听的端口,或者是正在侦听的unix套接字路径。
你的主应用程序文件如何:
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);
正如Brad所述,Express会公开类似于从 #createServer返回的对象()
,然而,TJ一直在认真考虑在将来的版本中放弃任何包含HTTP模块。使用上面的代码将来是安全的。
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.
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.
这篇关于访问node.js中的HTTP服务器对象表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!