Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Created May 15, 2017 22:18
Show Gist options
  • Save ernestofreyreg/b64594565f08bad2747ad43cfbb6eb4c to your computer and use it in GitHub Desktop.
Save ernestofreyreg/b64594565f08bad2747ad43cfbb6eb4c to your computer and use it in GitHub Desktop.
Simple backend to read contacts from a mongodb DB
require('dotenv').config()
import { MongoClient } from 'mongodb'
const connectMongoDB = () => MongoClient.connect(process.env.MONGODB)
const getContacts = (req, res) => {
return connectMongoDB()
.then(
db => db.collection('contacts')
.find({})
.toArray()
.then(documents => ({db, documents}))
)
.then(({db, documents}) => {
db.close()
return documents
})
.then(contacts => res.json(contacts))
.catch(err => res.status(400).send(err.toString()))
}
export const handler = getContacts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment