I have an http server and every time it gets a post request, it is supposed to insert the data into MongoDB. This server is supposed to be constantly running and accepting thousands of request in any given second.
How can I maximize the efficiency and speed of my code? I feel like my current code is not fast enough and furthermore wastes CPU power when it makes a new db every time it receives a request.
My current layout
var server = http.createServer(function (req, res) {
req.on('data', function(chunk) {
//Receive my data
});
req.on('end', function() {
//JSON parse my data
var db = new Db('test', new Server("111.111.11.111", 27017,{auto_reconnect: false}), {safe: true});
db.open(function(err, db) {
//Insert data into DB
db.close();
});
});
}); //End Http server
server.listen(8999);
I have tried replacing db.open with MongoClient.connect, but that considerably slows down processing and I don't know why. In this case, the older version of MongoDB Native for node js seems to work faster.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…