Error connecting to mongodb with mongoose on Google Compute Engine

I am following a bookshelf textbook . I was able to deploy the application and start my server and connect it to the mongodb instance. Everything is working fine. When I tried to deploy my own application, I followed all the steps, I was able to get my code in the cloud storage. I launched the linux virtual virtual machine and provided it with a startup script that I found in the tutorial, but when I went to the address

http://<your-instance-ip>:8080

I SHOULD NOT CONNECT TO MY APPLICATION. I SSHd into my instance and was able to go to the directory in which my file was saved, and I manually entered the command

node app.js

I immediately received a console log that my application runs on port 8080. But after 10 minutes I received an error message that my application could not connect to the mongodb instance. I use mongoose to connect to mongodb, whereas in the tutorial they used MongoClient. HERE MY CODE:

mongoose.connect('mongodb://***.***.***.***:27017/myappdatabase');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:')); 
db.once('open', function(callback) {
    console.log("connected to mongodb...");
});

and here is the tutorial code:

MongoClient.connect(url, function(err, db) {
  if (err) {
    console.log(err);
    return cb(err);
  }
  collection = db.collection(collectionName);
  cb(null, collection);
});

WHERE url EXACTLY VERY LIKE URL IN MONGOOSE.CONNECT ();

I am sure this is the only problem. When the VM rotates, it executes my app.js file and when it cannot connect to mongodb, my server crashes.

+2
source share

Source: https://habr.com/ru/post/1686761/


All Articles