I´m new to React and Node and even though couple of days there was no error in my code, today I started to get an error in my terminal and I can not connect to my mongo database. I run my server with node server.js command but I get the error. Anyone can help to find a solution for this? Thanks!
ERROR
Server is running on port 5000 (node:3339) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (node:3339) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00-z8g0w.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster0-shard-00-00-z8g0w.mongodb.net:27017 closed at TLSSocket. (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connection.js:372:9) at Object.onceWrapper (events.js:300:26) at TLSSocket.emit (events.js:210:5) at net.js:659:12 at TCP.done (_tls_wrap.js:481:7) { name: 'MongoNetworkError', }] at Pool. (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/topologies/server.js:433:11) at Pool.emit (events.js:210:5) at /Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/pool.js:577:14 at /Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/pool.js:1021:9 at callback (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connect.js:93:5) at /Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connect.js:113:7 at _callback (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connect.js:320:5) at Connection.errorHandler (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connect.js:336:5) at Object.onceWrapper (events.js:300:26) at Connection.emit (events.js:210:5) at TLSSocket. (/Users/petka/Desktop/react-recipesnew/node_modules/mongodb/lib/core/connection/connection.js:370:12) at Object.onceWrapper (events.js:300:26) at TLSSocket.emit (events.js:210:5) at net.js:659:12 at TCP.done (_tls_wrap.js:481:7) (node:3339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:3339) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
server.js
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const server = http.createServer(app)
//create the socket using the service instance
const io = socketIO(server)
io.on('connection', socket => {
console.log('New user connected.')
socket.on('infoEvent', (information) => {
console.log(`Information received: ${information}`)
io.sockets.emit('infoEvent', information)
})
socket.on('disconnect', () => {
console.log('User disconnected')
})
})
require('dotenv').config();
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser:true, useCreateIndex:true});
const connection = mongoose.connection;
//when the connection is open
connection.once('open', () => {
console.log("MongoDB connection established successfully")
})
const userrecipesRouter = require('./routes/userrecipes');
const usersRouter = require('./routes/users');
//require and use the files route
app.use('/userrecipes', userrecipesRouter);
app.use('/users', usersRouter);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});