访问node.js中的HTTP服务器对象表达式 [英] access to HTTP server object in node.js express

查看:72
本文介绍了访问node.js中的HTTP服务器对象表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个中间件(函数(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆