DEV Community

Discussion on: Setting up Node js, Express, Mongodb and Mongoose with TypeScript

Collapse
 
nshathish profile image
Shathish

why do you have app.listen in two places ?

Collapse
 
sgvlad profile image
Vlad

why?

Collapse
 
anshumanpadiya profile image
Anshuman Padiya

Because we need to be connected to our DB in order to serve the data to the client when they send a http request

Collapse
 
brogrammer_qazi profile image
Brogrammer Qazi

It is a mistake the correct method is to show server running message on terminal only when db is connected. like this
mongoose.connect(process.env.URI,{
useNewUrlParser:true,
useUnifiedTopology:true,
useCreateIndex:true,
useFindAndModify:false})
.then(()=>{
app.listen(port,host,()=>{
console.log(http://${host}:${port});
})
console.log("Connected")
})
.catch((err)=>console.log(err))