如何访问express 3路由器中的http.Server.connection属性?

问题描述 投票:0回答:2

我想在我的 Express 3 路线中使用 http.Server.connections 吗? 难道就没有办法再获取了吗

我应该 app.set('服务器', 服务器);

express.createServer() 已弃用,express 应用程序不再继承自 http.Server

var app = express(),
server = http.createServer(app);
server.listen(8080);
...
module.exports = function (app) {

    app.get('/connections', function (req, res) {
        res.send({
            connections: app.connections 
            // app != http.Server in express 3
        });
    });

};
node.js express httpconnection
2个回答
3
投票

您需要发送

app.connections
,而不是
server.connections
,因为您已经使用了:

server = http.createServer(app);

所以你的代码变成:

app.get('/connections', function (req, res) {
    res.send({
        connections: server.connections 
    });
});

0
投票
app.locals.connections = app.connections

然后在您的回复中:

res.send({ connections: res.locals.connections});
© www.soinside.com 2019 - 2024. All rights reserved.