Created
August 25, 2019 14:41
-
-
Save binyamin/75ad7de1126cd4ac94d43ea4b5fbcea0 to your computer and use it in GitHub Desktop.
MongoDB Config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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