I would like to deploy a Node.JS app on Cloud Foundry. I follow the following steps:
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" }
I create the manifest.yml
--- applications: - name: Policy_Studio memory: 2048MB env: NODE_ENV: production
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.");
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") }) };
connect by command line to SCAPP and push the app with cf push
As i don't have the MongoDB on the cloud i have an error
I build a MOngoDB service on the cloud and bind directly the app through the web GUI
Database connection responded with: failed to connect to server [2xtorvw9ys7tg9pc.service.consul:49642] on first connect [MongoError: connect ECONNREFUSED 10.98.250.54:49642]
Still the same error as in point 9
I tried to change the connection in install.js
Thank you for your help
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