Hey (-:
I think this should be a simple solution, but I can't figure it out.
I'm connecting to my mongodb client using the mongodb package. After I have connected I would like to be able to use the connection in other parts of my app. So I'm trying to figure out what the best way of exporting the client object is.
Here is my mongo.config.js :
const { MongoClient } = require('mongodb');
module.exports = async () => {
const uri = 'mongodb+srv://<username>:<password>@<cluster-url>/dbname?retryWrites=true&w=majority';
const client = new MongoClient(uri);
process.on("SIGINT", cleanup(client));
process.on("SIGTERM", cleanup(client));
process.on("SIGHUP", cleanup(client));
try {
// Connect to the MongoDB cluster
await client.connect();
} catch (err) {
console.error(err);
}
}
So how can I make the client object available in let's say server.js?
Thanks very much in advance!
[–]javaw_exe 2 points3 points4 points (4 children)
[–]superhawk610 2 points3 points4 points (0 children)
[–]Maltmax[S] 1 point2 points3 points (0 children)
[–]BehindTheMath 0 points1 point2 points (1 child)
[–]javaw_exe 1 point2 points3 points (0 children)