且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Nodejs HTTP服务器处理插入DB的多个请求

更新时间:2022-01-13 23:43:37

你想要转移到在启动期间打开由HTTP请求处理程序共享的大型连接池的方法。要调整MongoDB连接池大小以满足您的任何可伸缩性需求,请传递 options 参数 MongoClient.connect 来电。

You'll want to shift to an approach where you open a large pool of connections during startup that are shared by your HTTP request handlers. To tweak the MongoDB connection pool size to suit whatever scalability needs you have, pass an options parameter to your MongoClient.connect call.

var options = {
    server: {
        // The number of pooled connection available to your app.
        poolSize: 100
    }
};

mongodb.MongoClient.connect('mongodb://111.111.11.111/test', options, 
    function(err, db) {
        var server = http.createServer(function (req, res) {
            // Your req.on calls go here, directly using db rather than creating
            // new instances. Don't close db either.
        });
        server.listen(8999);  
    }
);