I am trying to create some APIs for my mobile app. I'm using Node+Express+Mongo+Firebase. Based on suggestions give in various other places (How to properly reuse connection to Mongodb across NodeJs application and modules) I've created below code structure:
This is what I have in my mongoUtils.js:
const MongoClient = require('mongodb').MongoClient
const uri = "mongodb+srv://user:password@blabla.azure.mongodb.net/bla?retryWrites=true&w=majority";
let _db
const connectDB = async (callback) => {
try {
MongoClient.connect(uri,{ useNewUrlParser: true }, (err, client) => {
_db = client.db('ABC');
return callback(err)
})
} catch (e) {
throw e
}
}
const getDB = () => _db
const disconnectDB = () => _db.close()
module.exports = { connectDB, getDB, disconnectDB }
This is what I have in my index.js:
const functions = require('firebase-functions');
const express = require('express');
const app = express();
var cors = require('cors');
app.use(cors());
const admin = require('firebase-admin');
const bodyParser = require('body-parser')
const assert = require('assert');
const MongoDB = require('./db/mongoUtils')
admin.initializeApp(functions.config().firebase);
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.post('/getUserByPhone',(request,response)=>{
MongoDB.connectDB(async (err) => {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = MongoDB.getDB();
const collection = db.collection('users');
console.log(request.body.phone);
// Find some documents
collection.find({/*Query here*/}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs)
response.send(docs);
})
})
})
app.post('/getUserById',(request,response)=>{
var ObjectId = require('mongodb').ObjectId;
var id = request.body.userId;
var userId = new ObjectId(id);
MongoDB.connectDB(async (err) => {
assert.equal(null, err);
const db = MongoDB.getDB();
console.log("Connected correctly to server");
const collection = db.collection('users');
collection.find({/*Query*/}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs)
response.send(docs);
})
})
})
app.post('/moreFunction',async(request,response)=>{
var currTime = new Date();
MongoDB.connectDB(async (err) => {
assert.equal(null, err);
const db = MongoDB.getDB();
const collection = db.collection('coll_2');
collection.find().toArray(function(err, docs) {
assert.equal(err, null);
response.send(docs);
});
})
})
app.post('/anotherFunction',async(request,response)=>{
MongoDB.connectDB(async (err) => {
assert.equal(null, err);
const db = MongoDB.getDB();
console.log("Connected correctly to server");
const collection = db.collection('coll_name');
collection.find().toArray(function(err, docs) {
assert.equal(err, null);
response.send(docs);
});
})
})
exports.api = functions.https.onRequest(app);
Now, whenever I am testing my app by running it on my Android device, I am seeing a spike of connections in my MongoDb console. Instead of one connection, I am seeing app use around 25-30 connections as I navigate between screens. What's wrong with my approach? Why does it use so many connections?
This app is not published and I'm the only user.