Search code examples
node.jsmongodbcodenvy

Not Connecting to Mongodb


I'm using cloud IDE Codenvy and i'm trying to connect to mongodb w/ node.js, but i'm not getting any response whether the connection failed or not. I've googled everything i can think of, but i'm new to this so i'm not sure if i'm missing something in Codenvy or i'm just not searching for the right questions. What am i missing or not understanding? Any help would be greatly appreciated.

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
Testing Mongodb Connection.
<script>

var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb://USERID:[email protected]:27017,cluster0-shard-00-01-ytx6z.mongodb.net:27017,cluster0-shard-00-02-ytx6z.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin";

MongoClient.connect(uri, function(err,db) {
    if (err) {
        document.write('err');
    }
    else {
        document.write ('connected');
    }

});
</script>

</body>
</html>

Solution

  • You cannot connect MongoDB from Frontend html page

    MongoDB isn't designed to expose data directly to the client. The client interacts with the application on the webserver, and webserver communicates with the database.

    Read more