Imdadul Haque
Imdadul Haque

Reputation: 1855

Failed to connected MongoDB with server

I failed to connected MongoDB with server and the codes are given below, please concern below,

app.js:

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());

module.exports = app;

.env:

MONGODB_SERVER = mongodb://localhost:27017/burger-builder
PORT = 3001

server.js:

const dotenv = require('dotenv');
dotenv.config();
const app = require('./app');
const mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_SERVER, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,

})
     .then(() => console.log('Connect to MongoDB!'))
     .catch(err => console.log('MongoDB Connection Failed!'));

const port = process.env.PORT || 3001;

app.listen(port, () =>{
     console.log(`Listening on port ${port}`);
})

Please concern the attached file for checking the error.

Note: In .env file I connected with the location as burger builder but there is no any file . Is it the main probel to failed the MongoDB with the server?

Upvotes: 0

Views: 56

Answers (1)

Hoang Le
Hoang Le

Reputation: 58

  1. Can you console log the error?
  2. Can you make sure mongodb is running?
  3. Can you make sure the uri is correct? port is correct?

Upvotes: 1

Related Questions