Skip to content

Instantly share code, notes, and snippets.

@vladi-strilets
Last active April 5, 2020 17:46
Show Gist options
  • Save vladi-strilets/c00c997657df1601554ff83b8434bb50 to your computer and use it in GitHub Desktop.
Save vladi-strilets/c00c997657df1601554ff83b8434bb50 to your computer and use it in GitHub Desktop.
mongodb client handler node.js
// db.js
const MongoClient = require("mongodb").MongoClient;
const url = "YOUR_URL";
var client;
const getClient = async () => {
if (client && client.isConnected()) {
console.log("DB CLIENT ALREADY CONNECTED");
} else
try {
client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("DB CLIENT RECONNECTED");
} catch (e) {
throw e;
}
return client;
};
module.exports = getClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment