Skip to content

Instantly share code, notes, and snippets.

@binyamin
Created August 25, 2019 14:41
Show Gist options
  • Save binyamin/75ad7de1126cd4ac94d43ea4b5fbcea0 to your computer and use it in GitHub Desktop.
Save binyamin/75ad7de1126cd4ac94d43ea4b5fbcea0 to your computer and use it in GitHub Desktop.
MongoDB Config
const {MongoClient} = require('mongodb');
let db, posts;
async function init() {
let uri = process.env.URI
const client = await MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
return await client.db('cms');
}
async function getPosts() {
if (!db) {
db = await init();
}
if (!posts) {
posts = db.collection('posts');
}
return posts;
}
module.exports = getPosts;
// Stuff
(async () => {
const posts = await getPosts();
// Get
router.get('/', async function(req, res) {
res.send(await posts.find({}).toArray())
})
// More Stuff
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment