Cloud9 + mongodb + nodejs

I am using cloud9 ide encoding a new project. When I deploy to cloudfoundry from cloud9ide. I have an error

Failed to start the application. Note that CloudFoundry uses a different port for listening. When calling 'listen ()' use it like '.listen (process.env.PORT || process.env.VCAP_APP_PORT)'.

This is my source

var port = (process.env.VMC_APP_PORT || 3000);

var host = (process.env.VCAP_APP_HOST || 'localhost');

var http = require ('http');

var env = process.env.VCAP_SERVICES? JSON.parse (process.env.VCAP_SERVICES): null;

var mongodata = env ['mongodb-1.8'] [0] ['credentials'];

http.createServer (function (req, res) {

res.writeHead (200, {'Content-Type': 'text / plain'});

res.end ('Hello World \ n' + env); }). listen (port, host);

This source has an error when I get the mongo object

var mongodata = env ['mongodb-1.8'] [0] ['credentials'];

But will this line succeed

Please help me!

Thank you very much

+1
source share
1 answer

Since the error in cloud9 console probably tells you (as it tells me when I try this :-)):

haalasdoallalsakdl (CloudFoundry): [5/6] Crash log ============/logs/stderr.log============ node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Cannot read property '0' of undefined at Object.<anonymous> (/var/vcap/data/dea/apps/haalasdoallalsakdl-0-8be0d413a9ec29a79f665d388ce414bd/app/server.js:7:35) at Module._compile (module.js:432:26) at Object..js (module.js:450:10) at Module.load (module.js:351:31) at Function._load (module.js:310:12) at Array.0 (module.js:470:10) 

Thus, the entry in VCAP_SERVICES is called so. When I console.log variable process.env , there are no services in it.

So, you will need to install the mongodb service for your application. The fastest way to do this is with VM VMC tools (currently this cannot be done in cloud9, so you have to install it locally):

 vmc create-service mongodb --bind your_app_name 

Then it will start normally.

NB You can probably fix this in a .yml file, but I don't know how to do this :-)

0
source

All Articles