DEV Community

Discussion on: A crash course on Serverless APIs with Express and MongoDB

 
adnanrahic profile image
Adnan Rahić

You see the isConnected variable?
That's what's checking if the mongoose connection is established. If it's true then mongoose won't connect again. 😄

Thread Thread
 
srujanjawaji profile image
srujanjawaji

The logic with isConnected variable, that I mentioned in my previous reply is from your other posts, but in this post you seem to have no used this logic with isConnected variable in the db.js, so that's why I was confused.
So are you saying this isConnected logic is needed so that every request doesn't create a new connection?

I understand that this is a 2 years old post & you may not remember what is in the code of this post versus the other posts of your.
So, request you to please check & confirm if the db.js code mentioned in this post also uses cached connection & if yes how?

For your quick reference below is the db.js code in this post:

// ./lib/db.js
const mongoose = require('mongoose')
mongoose.connect(process.env.DB)