Skip to content

Instantly share code, notes, and snippets.

@SahanAmarsha
Last active May 26, 2020 04:55
Show Gist options
  • Save SahanAmarsha/5291c0344314eb300bdbd1e476466459 to your computer and use it in GitHub Desktop.
Save SahanAmarsha/5291c0344314eb300bdbd1e476466459 to your computer and use it in GitHub Desktop.
How to establish connection between MongoDB and your Node.js backend using environmental variables
const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0-usg5d.mongodb.net`+
`/${process.env.DB_NAME}?retryWrites=true&w=majority`;
mongoose
.connect( url, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
.then(() =>{
app.listen(5000);
})
.catch(err => console.log( err ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment