How to connect MongoDB in Cloud9?

I have a problem with the MongoDB database in Cloud9 Please help solve this problem!

var MongoClient = require("mongodb").MongoClient; var port = process.env.PORT; var ip = process.env.IP; MongoClient.connect("mongodb://"+ip+":"+port+"/test",function(error,db){ if(!error){ console.log("We are connected"); } else{ console.dir(error); //failed to connect to [127.4.68.129:8080] } }); 

Output:

 Running Node Process Your code is running at 'http://demo-project.alfared1991.c9.io'. Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts! [Error: failed to connect to [127.4.68.129:8080]] 
+4
source share
3 answers

process.env.PORT and process.env.IP are the port and IP address for your application, not your database. You will want to pull your Mongo connection string from your MongoDB provider.

The following is an example hello world with Node.js homepage , modified to use two environment variables.

 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(process.env.PORT || 1337, process.env.IP || '127.0.0.1'); 
+2
source

If you follow https://docs.c9.io/setting_up_mongodb.html this link, you will install and run the mongodb daemon under your workspace.

And if you look at the output ./mongod , you will see this output:

 2015-08-22T12:46:47.120+0000 [initandlisten] MongoDB starting : pid=7699 port=27017 dbpath=data 64-bit host=velvetdeth-express-example-1804858 

Just copy the host and port value into the mongodb configuration, configure the database url, in this case:

 mongodb://velvetdeth-express-example-1804858:27017 
+7
source

For anyone facing this problem, the solution is here: https://docs.c9.io/setting_up_mongodb.html

MongoDB is pre-installed on the Cloud9 workspace. Run this:

 $ mkdir data $ echo 'mongod --bind_ip=$IP --dbpath=data --nojournal --rest " $@ "' > mongod $ chmod a+x mongod 

To start the Mongodb process, run:

 $ ./mongod 

Then run your node.js script application and you will go racing.

Here is what the following options mean:

- dbpath = data (since it is not available by default / var / db)

- nojournal, because mongodb usually pre-allocates a 2 GB log file (which exceeds Cloud9's quota of disk space)

- bind_ip = $ IP (because you cannot bind to 0.0.0.0)

- the rest is executed by default by default 28017

+1
source

All Articles