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
- 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.");
- 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") }) };
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
- On the gui i click restage button for my app
- 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]
- I add the service mongoDB in my manifest and cf push my application
Still the same error as in point 9
I tried to change the connection in install.js
Thank you for your help