Search code examples
node.jsmongodbcloud-foundryswisscomdev

Cloud Foundry MongoDB Error ECONNREFUSED


I would like to deploy a Node.JS app on Cloud Foundry. I follow the following steps:

  1. Add the engines part in the package.json

    {
      "name": "vsapc",
      "version": "1.0.0",
      "description": "Application Name",
      "main": "server/app.js",
      "scripts": {
        "start": "node server/app.js",
        "backup": "node backup.js",
        "restore": "node restore.js",
        "seed": "node server/seed/Seed.js",
        "postinstall": "node install.js"
      },
      "directories": {
        "test": "test"
      },
      "dependencies": {
        "bcrypt-nodejs": "0.0.3",
        "body-parser": "^1.15.2",
      "cfenv": "^1.0.3",
        "express": "^4.14.0",
        "jsonwebtoken": "^7.1.9",
        "mongodb": "^2.2.5",
        "mongoose": "^4.6.3",
        "mongoose-seed": "^0.3.1",
        "morgan": "^1.7.0",
        "promise": "^7.1.1",
        "prompt": "^1.0.0",
        "winston": "^2.2.0",
        "winston-daily-rotate-file": "^1.4.0"
      },
      "engines": {
      "node": "6.11.*",
      "npm": "5.*"
      },
      "author": "",
      "license": "ISC"
    }
    
  2. I create the manifest.yml

---
applications:
- name: Policy_Studio
  memory: 2048MB

  env:
    NODE_ENV: production
  1. I used the following to connect in install.js:
const vcapServices = JSON.parse(process.env.VCAP_SERVICES);
            let mongoUrl = '';
            mongoUrl = vcapServices.mongodb[0].credentials.uri;

            mongoose.connect(mongoUrl,{useMongoClient: true}, function (err){
              if (err) {
                  console.log("Database connection responded with: " + err.message);
                  console.log("Is your server.config.json up to date?");
                  process.exit(1);
                  return
              }
              console.log("Connected to database.");
  1. and the following in app.js:
Server.prototype.connectDatabase = function (url) {
          mongoose.Promise = Promise;
          const vcapServices = JSON.parse(process.env.VCAP_SERVICES);
          let mongoUrl = '';
          mongoUrl = vcapServices.mongodb[0].credentials.uri;
          mongoose.connect(mongoUrl,{useMongoClient: true});
          mongoose.connection.on("error", function (err) {
              log.error(err)
          });
          mongoose.connection.once("openUri", function () {
              log.info("Connected to DB")
          })
      };
  1. connect by command line to SCAPP and push the app with cf push

  2. As i don't have the MongoDB on the cloud i have an error

  3. I build a MOngoDB service on the cloud and bind directly the app through the web GUI

  4. On the gui i click restage button for my app
  5. I have the error
Database connection responded with: failed to connect to server 
[2xtorvw9ys7tg9pc.service.consul:49642] on first connect [MongoError: 
connect ECONNREFUSED 10.98.250.54:49642]
  1. I add the service mongoDB in my manifest and cf push my application
  2. Still the same error as in point 9

  3. I tried to change the connection in install.js

Thank you for your help


Solution

  • Finally we have found the problem. The cloud foundry is not allowing to access the MongoDB service during the postinstall phase. So we changed it to prestart and it worked. Thank you for your help