简体   繁体   中英

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?

  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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM