cloud9 + mongodb + nodejs

I using cloud9 ide coding new project. When I deploy on cloudfoundry from cloud9ide. I have error

Application failed to start. Please note that CloudFoundry uses a different port to listen to. 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 Worldn' + env); }).listen(port, host);

This source have error when I get mongo object

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

But not have this line deploy successful

Please help me !!

Thanks so much


As the error in the 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)

So there is no entry in VCAP_SERVICES called like that. When I console.log the process.env variable, there isn't even any service listed.

So you'll have to install the mongodb service for your app. Fastest to do this is via the CF VMC tools (can't run this in cloud9 at the moment, so you'll have to install this locally):

vmc create-service mongodb --bind your_app_name

Then it'll start up fine.

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

链接地址: http://www.djcxy.com/p/52508.html

上一篇: 在Heroku上创建node.js应用程序时要模块?

下一篇: cloud9 + mongodb + nodejs