1

I am creating a chat app in Android with Nodejs and Mongodb.

I get this error:

(node:49148) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Mo nitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

This is the code of js file.

const SocketServer = require('websocket').server
const http = require('http')
const express = require('express')
const app = express()
const mongoClient = require('mongodb').MongoClient

const server = http.createServer((req, res) => {})
const url = "mongodb://localhost:27017"


server.listen(50000, ()=>{
    console.log("Listening on port 50000...")
})

app.listen(60000, () => {
    console.log("Listening on port 60000...")
})

app.use(express.json())

mongoClient.connect(url,  (err, db) => {

    if (err) {
        console.log("Error while connecting mongo client")
    } else {

        const myDb = db.db('myDb')
        const collection = myDb.collection('myTable')

        app.post('/signup', (req, res) => {

            const newUser = {
                name: req.body.name,
                email: req.body.email,
                password: req.body.password
            }

            const query = { email: newUser.email }

            collection.findOne(query, (err, result) => {

                if (result == null) {
                    collection.insertOne(newUser, (err, result) => {
                        res.status(200).send()
                    })
                } else {
                    res.status(400).send()
                }

            })

        })

        app.post('/login', (req, res) => {

            const query = {
                email: req.body.email, 
                password: req.body.password
            }

            collection.findOne(query, (err, result) => {

                if (result != null) {

                    const objToSend = {
                        name: result.name,
                        email: result.email
                    }

                    res.status(200).send(JSON.stringify(objToSend))

                } else {
                    res.status(404).send()
                }

            })

        })

    }

})

wsServer = new SocketServer({httpServer:server})

const connections = []

wsServer.on('request', (req) => {
    const connection = req.accept()
    console.log('new connection')
    connections.push(connection)

    connection.on('message', (mes) => {
        connections.forEach(element => {
            if (element != connection)
                element.sendUTF(mes.utf8Data)
        })
    })

    connection.on('close', (resCode, des) => {
        console.log('connection closed')
        connections.splice(connections.indexOf(connection), 1)
    })

})
halfer
  • 19,471
  • 17
  • 87
  • 173

1 Answers1

0

this is just the warning shown by mongodb package to add option of { useUnifiedTopology: true } to connect function of mongodb. this is problem is already solved Please follow the link below

Warning on Connecting to MongoDB with a Node server