For context, I am writing an ExpressJS app and using the MongoDB node driver, mongodb.
The app will need to talk to the remote DB rapidly at times, depending on how often users are doing things. I understand that the app shouldn't open and close a connection each time it needs data. It should connect once, and there should be an thing named db that you can use for querying, example: db.collection("users").find(...), and all future DB calls should just use that db variable.
But if I deploy my app, I am assuming that server.js just executes once at the beginning of the app's life. That would mean that one connection is made once in the entire lifespan of that deployment.
But don't these connections time out? Do I just check that the db var is an open connection, and if not, reconnect before making a call?
Surprisingly, I haven't found many guides, so I don't know the right way, which is why I am asking here. Please let me know if I can ask this question in a better way.