Could not connect to MongoDB: MongoParseError: URI malformed, cannot be parsed events.js:287

Viewed 696

I am getting this when I run server.js:

Could not connect to MongoDB: MongoParseError: URI malformed, cannot be parsed
events.js:287
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::4000

This is my server.js:

const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const apiRoutes = require("./routes/api");
const PORT = process.env.PORT || 4000;
const app = express();

require('dotenv').config();

const mongoose = require('mongoose');

process.env.NODE_ENV === "production" ? app.use(express.static("../build")) : null;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use("/api", apiRoutes);

const uri = process.env.MONGODB_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true  });

const connection = mongoose.connection;
connection.once('open', () => {
 console.log("MongoDB database connection established successfully");
})

app.get("*", function(req, res) {
 res.sendFile(path.join(__dirname, "../build/index.html"));
});

app.listen(PORT, function() {
 console.log(`Server running on port ${PORT}`);
});
0 Answers
Related