Created
May 15, 2017 22:18
-
-
Save ernestofreyreg/b64594565f08bad2747ad43cfbb6eb4c to your computer and use it in GitHub Desktop.
Simple backend to read contacts from a mongodb DB
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
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